Reputation: 315
New to data persistence and I'm trying to improve the performance of an app that uses SwiftData. I profiled the app in Instruments, and I saw a lot of data / relationship faults.
What are they and how are they caused? How would I identify the source and fix them?
Looking through the stack trace and call trees, I can't make sense of anything.
Here is a simplified version of my data models:
@Model
class Store {
var id = UUID()
// relationships
@Relationship(deleteRule: .nullify, inverse: \Item.stores)
var items: [Item]?
@Relationship(deleteRule: .cascade, inverse: \ShelfLocation.store)
var shelfLocations: [ShelfLocation]?
init() {}
}
@Model
class Item {
var id = UUID()
@Attribute(.unique) var upcCode: String?
var stores: [Store]?
var shelfLocations: [ShelfLocation]?
init() {}
}
@Model
class ShelfLocation {
var isle: Int = 0
var shelf: Int = 0
var store: Store?
@Relationship(deleteRule: .nullify, inverse: \Item.shelfLocations)
var item: Item?
init(isle: Int, shelf: Int, item: Item) {
self.isle = isle
self.shelf = shelf
self.item = item
}
}
Note: I chose to have the shelf location property separate from the item, because the same item could be found in multiple stores and have different locations. I'm not sure if this triangular relationship is ideal and if there could be a better way to do this.
Upvotes: 0
Views: 14