SwiftData ModelContainer can not be created in iOS 17.4 Beta

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)
Answered by mikrasya in 780347022

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.

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 brandnew 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 []
        }

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!

Accepted Answer

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.

Thanks @mikrasya for documenting this. I have the same problem, but already have inverses set. Removing inverses didn't help either. (I've read that inverses aren't necessary when using iCloud, that they are automatically inferred). 🤷🏼‍♂️

I sent in a question to Developer Tech Support, and will repeat any solution here for you and the other five who've marked your posting with "Same Here".

I tried creating a new app, with a new container, using the Xcode 'stock' Item ie; item.timestamp. I tried it in a 17.4 simulator and iPhone with 17/4. Both failed for the same reason as noted by the OP. Fatal error: Could not create ModelContainer: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer). I also have a "Live" App that is not loading because of 17.4. Also... I have played with the relationships as described in the posts above and nothing worked. Again - Apple's stock, stripped down, Items app is failing.???

Update: As for my app... The culprit was that I had added modelContext into the environment using both an @Environment call and .modelContainer on WindowGroup. By the way, this worked prior to 17.4. The solution was to remove : @Environment(.modelContext) private var modelContext.

As for the 'stock app', I discovered that the issue was that timestamp is not marked optional so it fails. Marking it optional? and then providing ?? Date.now values where needed solved the problem.

In case anyone else stumbles across this, I found the following syntax resolved the error for me:

    @Relationship(inverse: \Item.category)
    var items: [Item]? = []
    var category: ItemCategory?

Seems to work so I guess it's correct? 🤷

SwiftData ModelContainer can not be created in iOS 17.4 Beta
 
 
Q