I'm using SwiftData with CloudKit and have been trying to migrate from SchemaV1 to SchemaV2, but it seems reducing the Entities crashes my app.
// Example of migrating from V1 to V2
// Dropping `Person` because it's no longer needed
do {
// SchemaV1: Person.self, Author.self
// SchemaV2: Author.self
let schema = Schema(versionedSchema: SchemaV2.self)
return try ModelContainer(
for: schema,
migrationPlan: AppSchemaMigrationPlan.self,
configurations: ModelConfiguration(
cloudKitDatabase: .automatic)
)
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
Is it possible to drop Entities in the Schema Migration Plan?
How can I delete the Person
model from my Schema and CloudKit?
Are you on the CloudKit production environment or the development one? On the production environment, you will want to keep the model class there because:
-
CloudKit production environment only supports additive changes, and so the CloudKit schema associated with the SwiftData model will always be there. See Deploying an iCloud Container’s Schema for more information.
-
An app instance with the
SchemaV2
may still need to handleSchemaV1
data because a user may not update your app on all their devices simultaneously. Although "handle" here pretty much means "ignore," SwiftData + CloudKit still needs to know thatPerson
is a valid type.
If you are on the development environment, you can try to reset the CloudKit environment, and go from the very beginning.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.