When I tried to use a working project with iOS 18 installed on my device, it wouldn't work anymore and crash right away. Before with iOS 17 it was working fine.
I can't access child variables that are saved in an Array in a parent object in SwiftData. The error is always somewhere in these hidden lines:
{
@storageRestrictions(accesses: _$backingData, initializes: _title)
init(initialValue) {
_$backingData.setValue(forKey: \.title, to: initialValue)
_title = _SwiftDataNoType()
}
get {
_$observationRegistrar.access(self, keyPath: \.title)
return self.getValue(forKey: \.title)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: \.title) {
self.setValue(forKey: \.title, to: newValue)
}
}
}
The child classes are also inserted and saved into the modelContext when created and set to the parent instance, but I also can't fetch them via modelContext.fetch() - Error here is:
Thread 1: EXC_BREAKPOINT (code=1, subcode=0x243a62a4c)
Maybe there is a problem with the relationship between two saved instances. The parent instances are saved correctly and it was working in iOS 17.
The problem is similar to these two cases: https://forums.developer.apple.com/forums/thread/762679 https://forums.developer.apple.com/forums/thread/738983
I changed the logic after I reviewed these threads, as I am now linking the parent and child instances, that got rid of one warning in the console.
button.canvas = canvas
modelContext.insert(button)
canvas.buttons = [button]
But in the end those threads were not enough for me to find a fix for my problem.
A small project can be found here: https://github.com/DonMalte/SwiftDataTest