I'm implementing SwiftData with inheritance in an app.
I have an Entity class with a property name. This class is inherited by two other classes: Store and Person. The Entity model has a one-to-many relationship with a Transaction class.
I can list all my Entity models in a List with a @Query annotation without a problem. However, then I try to access the name property of an Entity from a Transaction relationship, the app crashes with the following error:
Thread 1: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0x96530ce28d41eb63 <x-coredata://DABFF7BB-C412-474E-AD50-A1F30AC6DBE9/Person/p4>))) with Optional(F07E7E23-F8F0-4CC0-B282-270B5EDDC7F3)
From my attempts to fix the issue, I noticed that:
- The crash seems related to the relationships with classes that has inherit from another class, since it only happens there.
- When I create new data, I can usually access it without any problem. The crash mostly happens after reloading the app.
This error has been mentioned on the forum (for example here), but in a context not related with inheritance.
You can find the full code here.
For reference, my models looks like this:
@Model
class Transaction {
@Attribute(.unique)
var id: String
var name: String
var date: Date
var amount: Double
var entity: Entity?
var store: Store? { entity as? Store }
var person: Person? { entity as? Person }
init(
id: String = UUID().uuidString,
name: String,
amount: Double,
date: Date = .now,
entity: Entity? = nil,
) {
self.id = id
self.name = name
self.amount = amount
self.date = date
self.entity = entity
}
}
@Model
class Entity: Identifiable {
@Attribute(.preserveValueOnDeletion)
var name: String
var lastUsedAt: Date
@Relationship(deleteRule: .cascade, inverse: \Transaction.entity)
var operations: [Transaction]
init(
name: String,
lastUsedAt: Date = .now,
operations: [Transaction] = [],
) {
self.name = name
self.lastUsedAt = lastUsedAt
self.operations = operations
}
}
@available(iOS 26, *)
@Model
class Store: Entity {
@Attribute(.unique) var id: String
var locations: [Location]
init(
id: String = UUID().uuidString,
name: String,
lastUsedAt: Date = .now,
locations: [Location] = [],
operations: [Transaction] = []
) {
self.locations = locations
self.id = id
super.init(name: name, lastUsedAt: lastUsedAt, operations: operations)
}
}
In order to reproduce the error:
- Run the app in the simulator.
- Click the + button to create a new transaction.
- Relaunch the app, then click on any transaction.
The app crashes when it tries to read te name property while building the details view.