Change to SwiftData ModelContainer causing crashes

I have some models in my app:

[SDPlanBrief.self, SDAirport.self, SDChart.self, SDIndividualRunwayAirport.self, SDLocationBrief.self]

SDLocationBrief has a @Relationship with SDChart

When I went live with my app I didn't have a versioned schema, but quickly had to change that as I needed to add items to my SDPlanBrief Model.

The first versioned schema I made included only the model that I had made a change to.

static var models: [any PersistentModel.Type] {
        [SDPlanBrief.self]
    }

I had made zero changes to my model container and the whole time, and it was working fine. The migration worked well and this is what I was using:

.modelContainer(for: [SDAirport.self, SDIndividualRunwayAirport.self, SDLocationBrief.self, SDChart.self, SDPlanBrief.self])

I then saw that to do this all properly, I should actually include ALL of my @Models in the versioned schema:

enum AllSwiftDataSchemaV3: VersionedSchema {
    
    static var models: [any PersistentModel.Type] {
        [SDPlanBrief.self, SDAirport.self, SDChart.self, SDIndividualRunwayAirport.self, SDLocationBrief.self]
    }

    static var versionIdentifier: Schema.Version = .init(2, 0, 0)
}

extension AllSwiftDataSchemaV3 {
    
    @Model
    class SDPlanBrief {
        var destination: String
        etc...
        
        init(destination: String, etc...) {
            self.destination = destination
            etc...
        }
    }
    
    @Model
    class SDAirport {
        var catABMinima: String
        etc...
        
        init(catABMinima: String etc...) {
            self.catABMinima = catABMinima
            etc...
        }
    }
    
    @Model
    class SDChart: Identifiable {
        var key: String
        etc...
        var brief: SDLocationBrief?  // @Relationship with SDChart
        
        init(key: String etc...) {
            self.key = key
            etc...
        }
    }
    
    @Model
    class SDIndividualRunwayAirport {
        var icaoCode: String
        etc...
        
        init(icaoCode: String etc...) {
            self.icaoCode = icaoCode
            etc...
        }
    }
    
    @Model
    class SDLocationBrief: Identifiable {
        var briefString: String
        etc...

        @Relationship(deleteRule: .cascade, inverse: \SDChart.brief) var chartsArray = [SDChart]()

        init(
            briefString: String,
            etc...
            chartsArray: [SDChart] = []
        ) {
            self.briefString = briefString
            etc...
            self.chartsArray = chartsArray
        }
    }
}

This is ALL my models in here btw.

I saw also that modelContainer needed updating to work better for versioned schemas. I changed my modelContainer to look like this:

actor ModelContainerActor {
    
    @MainActor
    static func container() -> ModelContainer {
        let schema = Schema(
            versionedSchema: AllSwiftDataSchemaV3.self
        )
        let configuration = ModelConfiguration()
        let container = try! ModelContainer(
            for: schema,
            migrationPlan: PlanBriefMigrationPlan.self,
            configurations: configuration
        )
        return container
    }
}

and I am passing in like so:

.modelContainer(ModelContainerActor.container())

Each time I run the app now, I suddenly get this message a few times in a row:

CoreData: error: Attempting to retrieve an NSManagedObjectModel version checksum while the model is still editable. This may result in an unstable verison checksum. Add model to NSPersistentStoreCoordinator and try again.

I typealias all of these models too for the most recent V3 version eg:

typealias SDPlanBrief = AllSwiftDataSchemaV3.SDPlanBrief

Can someone see if I am doing something wrong here? It seems my TestFlight users are experiencing a crash every now and then when certain views load (I assume when accessing @Query objects). Seems its more so when a view loads quickly, like when removing a subscription view where the data may not have had time to load??? Can someone please have a look and help me out.

I am wondering if having my container set up like this would be more correct?

var sharedModelContainer: ModelContainer = {
    let schema = Schema(
        [
            SDPlanBrief.self,
            SDAirport.self,
            SDChart.self,
            SDIndividualRunwayAirport.self,
            SDLocationBrief.self
        ]
    )
    let configuration = ModelConfiguration()
    do {
        return try ModelContainer(for: schema,
                                  migrationPlan: PlanBriefMigrationPlan.self,
                                  configurations: configuration)
    } catch {
        fatalError("error = \(error.localizedDescription)")
    }
}()

Same issue was noted in https://developer.apple.com/forums/thread/793323

The issue is known but if you continue to encounter the issue on the latest beta previews, please file a file a feedback report and share your report ID here.

Thank you for your response. I had actually read that post in depth before posting here. What I would really like to know is, am I doing anything wrong with my VersionedSchema, ModelContainer or @Models please? I have been looking all around and it is hard to find the right answer for my specific case where I have transitioned to a Versioned Schema where I have suddenly placed all my @Models in, and have also changed my ModelContainer. I actually initially built this new container like so as I thought this was correct for migration plans:

actor ModelContainerActor {

@MainActor
static func container() -> ModelContainer {
    let schema = Schema(
        versionedSchema: AllSwiftDataSchemaV3.self
    )
    let configuration = ModelConfiguration()
    let container = try! ModelContainer(
        for: schema,
        migrationPlan: PlanBriefMigrationPlan.self,
        configurations: configuration
    )
    return container
}

}

Which I believe may have been causing the crashes. I haven't had any crashes in the last 24 hours since changing to this container:

var sharedModelContainer: ModelContainer = {
    let schema = Schema(
        [
            SDPlanBrief.self,
            SDAirport.self,
            SDChart.self,
            SDIndividualRunwayAirport.self,
            SDLocationBrief.self
        ]
    )
    let configuration = ModelConfiguration()
    do {
        return try ModelContainer(for: schema,
                                  migrationPlan: PlanBriefMigrationPlan.self,
                                  configurations: configuration)
    } catch {
        fatalError("error = \(error.localizedDescription)")
    }
}()

Your advice/analysis would be greatly appreciated, thank you so much.

Moving your Models to a VersionedSchema should have no adverse impact. The main thing is to remember to keep the changes you ship encapsulated in the corresponding VersionSchema for each shipping release of your App.

Checkout this talk on more about VersionedSchemas: https://developer.apple.com/videos/play/wwdc2025/291/

Thank you very much. Lastly, do you agree this is a perfectly good ModelContainer for my situation?

var sharedModelContainer: ModelContainer = {
    let schema = Schema(
        [
            SDPlanBrief.self,
            SDAirport.self,
            SDChart.self,
            SDIndividualRunwayAirport.self,
            SDLocationBrief.self
        ]
    )
    let configuration = ModelConfiguration()
    do {
        return try ModelContainer(for: schema,
                                  migrationPlan: PlanBriefMigrationPlan.self,
                                  configurations: configuration)
    } catch {
        fatalError("error = \(error.localizedDescription)")
    }
}()

Or is this, with the versioned schema plugged in more appropriate:

let modelContainer: ModelContainer = {
    do {
        let schema = Schema(versionedSchema: AllSwiftDataSchemaV3.self)
        return try ModelContainer(
            for: schema,
            migrationPlan: PlanBriefMigrationPlan.self
        )
    } catch {
        fatalError("ModelContainer failed: \(error.localizedDescription)")
    }
}()
Change to SwiftData ModelContainer causing crashes
 
 
Q