SwiftData Inheritance Query Specialized Model

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?

Answered by DTS Engineer in 855578022

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.

You haven’t provided the details about CollectionItem which seems to be the class that causes the crash but I must say that it’s a very strange design you have where the superclass both has a children property of self and is being inherited. Do you really need such a complex solution?

Here is the CollectionItem model:

@available(iOS 26, *)
@Model final class CollectionItem: Item {
	var name: String
	var icon: String?

	init(
		name: String,
		icon: String? = nil,
		parent: CollectionItem? = nil,
		timestamp: Date = .now
	) {
		self.name = name
		self.icon = icon
		super.init(parent: parent)
	}
}

Do you really need such a complex solution?

I'm not sure, what I want is to mix CollectionItem and LinkItem. Collections can have children, Links cannot.

It's like a file system where a directory can have directories or files.

So yeah it's a bit complex but this might not be that exotic.

There is probably a better way to design this…

Well a simplified version could be to only keep the current super class Item and use a Boolean (or an enum) property to define if it’s a collection or a link.

The use of the inheritance, as shown in the above code snippet, looks fine to me, and I don't really see a crash by running the code on my iOS 26 Beta 7 simulator and device. So did you try with Beta 7, the latest Beta as of today, yet?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi Ziqiao, I've tried with iOS 26 Beta 7 and since Beta 8 (landed tonight) on device (because my Xcode is stuck on iOS Beta 6, don't know why) and the problem is solved. I can now query CollectionItem with predicate that contains inherited properties.

Now I have only got problems with accesses to the children property in List.

When i got the following hierarchy and the SwiftUI view List access to the \Item.children property I got another kind of crash.

  • Category: "Work" (parent: none)
  • Category: "Tech" (parent: "Work")
Thread 1: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0x9872c0007ba42159 <x-coredata://E5407415-7D6A-4920-B910-6D0D39290E5E/CollectionItem/p1>))) with Optional(7639D151-5D48-46AC-AB2A-7F0F50919AC5)

But I guess it's a different kind of problem now.

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.

Thanks a lot for helping Ziqiao, i've made a small iOS App that shows the issue which is available here: https://github.com/fgirardey/SwiftDataInheritance

The step to reproduce are:

  • Create a collection named "Work"
  • Create another collection name "Tech" that has "Work" as a parent
  • Kill the app, relaunch and see the crash happening when List is accessing to \children through \.childrenOrNil.

That's most likely a bug happening when creating a hierarchical list using List(… children) + SwiftData. Would you mind to file a feedback report for us with your reproducible case, and share your report ID here? Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

FB19957253 (Crash on Hierarchical List children access from SwiftData inherited model)

Again, thank you so much! I hope I gave enough informations, feel free to ask if I can do anything.

SwiftData Inheritance Query Specialized Model
 
 
Q