@DTS Engineer I have created Feedback, FB15481013, and attached a sample project showing the issue.
Post
Replies
Boosts
Views
Activity
I was able to create a sample project this time showing the error. However, I'm unable to attach a zip file here, looks like only .json files are selectable. How can I send this to you?
I also found a post that had the same error, but with no resolution:
@Relationship crash on ios17.5 but not on ios18
In the meantime, I will take a look through the post you referenced.
Thank you!
I appreciate the reply. I saw it done both ways online and I left the @Relationship on Summary more as a reminder to me. But removing it gives me the same error.
I also removed the creation Summary from the Item initializer and created the Summary after init, but the same error remains so it's definitely something to do with the relationship.
And it still exists on 17.7.
@Lxdro @DTS Engineer
Did you get to the bottom of this issue? I am experiencing the exact same thing and would like to know if there's a work around.
All of the models are type aliased, so the extension will apply to any version of the model. Which is the whole point to avoid duplicating the functions and properties every time I create a new schema version
Here is my question again
If my @Model defines functions and computed properties, are those used in the checksum? Can those be moved to an extension to reduce code duplication or am I now required to carry those functions and properties along for every version I create?
I have two branches in my repository that can be used to replicate, but I will need your GitHub username to give you access
@DTS Engineer Hope you have a nice holiday weekend and we can get back to this next week
Hi Ziqiao!
I continue to get the error
error: NSLocalizedDescription : Cannot use staged migration with an unknown model version.
I thought everything was coming together, but I was mistakenly testing a migration from V1 to V2 instead of testing un-versioned migrating to V2.
I thought I read SwiftData uses a checksum to tell if something is the same. Is that true? If so, do we know how it calculates that value?
If my @Model defines functions and computed properties, are those used in the checksum? Can those be moved to an extension to reduce code duplication or am I now required to carry those functions and properties along for every version I create?
If I have RandomFile.swift and I define
@Model
final class Item {
var id: UUID = UUID()
var name: String
var isDefault: Bool
init(name: String, isDefault: Bool = false) {
self.name = name.localizedCapitalized
self.isDefault = isDefault
}
private func doSomething(_ val: Int) { }
var compProp: String {
return "x"
}
}
Then I create SchemaV1.swift with extracted @Models
enum SchemaV1: VersionedSchema {
static var versionIdentifier = Schema.Version(1, 0, 0)
static var models: [any PersistentModel.Type] {
[Item.self, Thing.self, Element.self, Piece.self]
}
@Model
final class Item {
var id: UUID = UUID()
var name: String
var isDefault: Bool
init(name: String, isDefault: Bool = false) {
self.name = name.localizedCapitalized
self.isDefault = isDefault
}
}
@Model final class Thing { ... }
@Model final class Element { ... }
@Model final class Piece { ... }
}
And change RandomFile.swift to include
extension Item {
private func doSomething(_ val: Int) { }
var compProp: String {
return "x"
}
}
Do I need to define all this extra stuff in my SchemaVx.swift file for every model? And again for every Vx I create?
Just wanted to update for anyone ending up here in the future. Making good progress with this technique after starting fresh.
I must have corrupted my original database with half migrations from the many things I was trying out.
I'm attempting to use my original code on a clean simulator to generate some data then apply the migration code to see if I get the same errors of if it now works properly.
If I still have issues, I will build a custom project to replicate. Until then, I've accepted the answer.
Thank you for the help
Ok Great!
I have created my VersionedSchemas that define my four models and included them in the models property. And created my ModelContainer using the schema initializer Schema(versionedSchema: SchemaV2.self)
Yet nothing works. I'm getting a few errors including:
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.
and
error: Store failed to load. <NSPersistentStoreDescription: 0x600003a85cb0> (type: SQLite, url: <redacted>
with error = Error Domain=NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown model version." UserInfo={NSLocalizedDescription=Cannot use staged migration with an unknown model version.} with userInfo {
NSLocalizedDescription = "Cannot use staged migration with an unknown model version.";
}
I’m concerned that I may have corrupted my database with all the things I’ve been trying. Is there a chance that the database is corrupted, or is there something fundamentally wrong in my setup that could be causing these issues?
Thank you for the reply, I'll take a look at the link you posted, but wanted to clarify my questions.
I currently have four models: Item, Thing, Element, Piece
creating the model container like so:
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
Thing.self,
Element.self,
Piece.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
Do all @Model classes need to be converted?
I plan on updating all models, but I was really asking what is necessary. And it ties into the following question
Is there one VersionedSchema for the entire app that handles all models or one VersionedSchema per model?
So I'm asking, do I create MyAppSchemaV1 and define the models there, or do I need to create ItemSchemaV1, ThingSchemaV1, ElementSchemaV1, and PieceSchemaV1 - then I would add ItemSchemaV2?
Finally you state
The schema used to create a model container should be the current version schema, in this case, version 2 schema.
This leads me to believe I should have a single schema for the entire app. Am I understanding things correctly so far?