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.