ModelContainer fails to create in ios18 beta

I get this error : ModelContainer creation failed: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer, _explanation: nil) AbsGod/AbsGodApp.swift:25: Fatal error: Failed to create ModelContainer: The operation couldn’t be completed. (SwiftData.SwiftDataError error 1.)

when doing:

struct MyAppApp: App {
    let container: ModelContainer

    init() {
        do {
            let schema = Schema([
                Workout.self,
                Exercise.self
            ])
            let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
            container = try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            print("ModelContainer creation failed: \(error)")
            fatalError("Failed to create ModelContainer: \(error.localizedDescription)")
        }
    }
    
    var body: some Scene {
        WindowGroup {
            TabBarView()
                .tint(.red)
        }
        .modelContainer(for: [Workout.self, Exercise.self])
    }

And I have no clue of how to resolve that. I saw similar issues with CloudKit, but I don't even have enabled it, just ticked Automatically manage signing in Signing & Capabilities of my project.

the relationship of my classes are defined like this, in a workout class having an array of exercises: @Relationship(deleteRule: .cascade, inverse: \Exercise.workout) var exercises: [Exercise]? = []

and the inverse in the exercise class: var workout: Workout?

I don't feel like the problem is coming from my classes, because when I try to reproduce the error on simpler project, everything works. it just won't create a modelContainder in my project and I have no idea what can makes that and the error is not really explicit...

Your code tries to create two model containers: one in MyAppApp.init(), the other via .modelContainer(for: [Workout.self, Exercise.self]).

To load a SwiftData store, you need only one container at a time. If you don't have any special configuration, consider removing the code in MyAppApp.init(). If you do, use .modelContainer(container) and make sure that container is created before being used.

If the error is still there, you might try to provide more information by:

  • Checking the userInfo of the error if there is a key named NSLocalizedFailureReason that indicates the reason of the failure.

  • Providing the project that reproduces the issue.

Without more information, folks can't tell what happens on your side.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

ModelContainer fails to create in ios18 beta
 
 
Q