Reputation: 1130
I have an app in the app store which is working fine. Some users reported that they have app crashing when they are using iOS 17.4 beta. I could recreate the issue and it is happening when ModelContainer is crerated. This code..
do {
let configuration = ModelConfiguration(for: MyTodo.self, isStoredInMemoryOnly: false)
modelContainer = try ModelContainer(for: MyTodo.self, configurations: configuration)
} catch {
fatalError("Failed to load model container.\(error)")
}
is throwing this error message:
Fatal error: Failed to load model container.SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer)
My model do not have any @Attribute(.unique)
All properties have default values Relationships are marked as optional App is using CloudKit for iCloud syncronization
A brand new app with the same model does not have this issue. When I uncheck "CloutKit" in Signing & Capabilities, the app launch normally.
Widget can obtain the data without error/crash from modelContainer using this code:
guard let modelContainer = try? ModelContainer(for: MyTodo.self) else {
return []
}
Edit: I have now identified that this issue is happening due to Relationships in my model. My MyToDo has task defined like this:
@Relationship(deleteRule: .cascade) var tasks: [MyTask]? = []
and MyTask has reference back as following:
var todo: MyTodo?
Both of them are optional as required for CloudKit. This model can be read in iOS 17.3, but in 17.4 beta, it crashes!
Upvotes: 0
Views: 535
Reputation: 1130
It seems for whatever reason inverse relationship needs to be explicitly defined. Change from this:
@Relationship(deleteRule: .cascade) var tasks: [MyTask]? = []
to
@Relationship(deleteRule: .cascade, inverse: \MyTask.todo) var tasks: [MyTask]? = []
has solved the crash issue. Works both in iOS 17.3 and 17.4.
Upvotes: 0