Can't access child entries of SwiftData class

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

Answered by DTS Engineer in 820698022

The crash doesn't happen on my iOS 18.2.1 device. Did you try with iOS 18.2.1, the latest public release? (The crash does happen on my iOS 18.1 device, btw, which seems like a regression.)

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The crash doesn't happen on my iOS 18.2.1 device. Did you try with iOS 18.2.1, the latest public release? (The crash does happen on my iOS 18.1 device, btw, which seems like a regression.)

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you Ziqiao for your answer! You are right it works with 18.2.1, I didn't bother to check because the issue was active for some iOS releases now.

But that leads me to two follow up questions:

  1. The issue is only fixed if I set my minimum target to 18.2 and that is quite a bad user experience. So there is no logic for me to fix and I have to accept that this is a bug?
  2. Was the issue or the change of some important CoreData logic changes from iOS 17 to iOS 18 somehow communicated or acknowledged somewhere? Because it seems even though in my case it wasn't enough, the linked forum post had multiple users with a similar problem, which some of them fixed with the help of this thread. But other than those 2 threads I couldn't find any information on this. I ask because if changes like these come up in the future I would like to know how to handle these.

Thanks for your help! :)

Can't access child entries of SwiftData class
 
 
Q