Hi,
I am currently experiencing some trouble when using parent model property in a predicate of a child model.
I have an Item class that define parent-child relationship:
@Model class Item {
var timestamp: Date
@Relationship(inverse: \Item.children)
var parent: Item?
var children: [Item]
init(parent: Item? = nil, children: [Item] = [], timestamp: Date = .now) {
self.parent = parent
self.children = children
self.timestamp = timestamp
}
}
I subclass this model like that:
@available(iOS 26, *)
@Model final class CollectionItem: Item { /* ... */ }
When i make a Query in my View like that the system crashes:
@Query(
filter: #Predicate<CollectionItem> { $0.parent == nil },
sort: \CollectionItem.name,
)
private var collections: [CollectionItem]
CrashReportError: Fatal Error in DataUtilities.swift
AppName crashed due to fatalError in DataUtilities.swift at line 85.
Couldn't find \CollectionItem.<computed 0x000000034005d4e8 (Optional<Item>)> on CollectionItem with fields [SwiftData.Schema.PropertyMetadata(name: "name", keypath: \CollectionItem.<computed 0x000000034003c120 (String)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "icon", keypath: \CollectionItem.<computed 0x000000034003ca04 (Optional<String>)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "timestamp", keypath: \Item.<computed 0x0000000340048018 (Date)>, defaultValue: nil, metadata: nil), SwiftData.Schema.PropertyMetadata(name: "parent", keypath: \Item.<computed 0x0000000340048a4c (Optional<Item>)>, defaultValue: nil, metadata: Optional(Relationship - name: , options: [], valueType: Any, destination: , inverseName: nil, inverseKeypath: Optional(\Item.<computed 0x0000000340048fe8 (Array<Item>)>))), SwiftData.Schema.PropertyMetadata(name: "children", keypath: \Item.<computed 0x0000000340048fe8 (Array<Item>)>, defaultValue: nil, metadata: nil)]
When I query as Item
it works but then i cannot sort on CollectionItem
field and must add unnecessary down casting:
@Query(
filter: #Predicate<Item> { $0.parent == nil && $0 is CollectionItem },
)
private var items: [Item]
Am I missing something? Is it a platform limitation or a known issue?
Nice to know that the original issue goes away in the latest Beta.
Regarding the crash triggered by the access to \Item.children
, if you can provide a runnable code snippet to demo the issue, I'll take another look.
You have mentioned a few classes with inheritance, and so I think it is probably worth mentioning that over-using inheritance may impact your app's performance negatively.
Concretely, with today's default store (DefaultStore), all the types in an inheritance hierarchy are persisted with one single table in SQLite
. If your inheritance hierarchy has many classes, the table (columns and data) can be quite big, which can slow down the performance of fetching and inserting data.
That being said, when adopting SwiftData inheritance, you might examine if that is a right use case. This WWDC25 session(starting from around 4:25) covers this topic a bit. You can take a look, if haven't yet.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.