SwiftData+Cloudkit Migration Failed

I have v3 models in coredata (model: Event, Lecture), and make new model in v4 with swiftdata but model name, property is different (model: EventModel, LectureModel).

For migration, I add V3Schema, V4Schema and MigrationPlan.

enum V4Schema: VersionedSchema {
    static var models: [any PersistentModel.Type] = [LectureModel.self, EventModel.self ]
    
    static var versionIdentifier = Schema.Version(4, 0, 0)
    
}

enum V3Schema: VersionedSchema {
    static var models: [any PersistentModel.Type] = [Event.self, Lecture.self]
    
    static var versionIdentifier = Schema.Version(3, 5, 2)
}
enum ModelMigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] = [V3Schema.self, V4Schema.self]
    static var stages: [MigrationStage] = [migrateV3ToV4]
    
}
extension ModelMigrationPlan {
    static let migrateV3ToV4 = MigrationStage.custom(fromVersion: V3Schema.self,
                                                     toVersion: V4Schema.self,
                                                     willMigrate: willMigrate,
                                                     didMigrate: { _ in Log.debug(message: "Migration Complete") })
}

private func willMigrate(context: ModelContext) throws {
    try migrateLectures(context: context)
    try migrateEvents(context: context)
    try context.save()
}

private func migrateEventTypes(context: ModelContext) throws {
    // create new v4 event model using v3 event model.
}

private func migrateLectures(context: ModelContext) throws {
    // create new v4 lecture model using v3 lecture model.
}
static let release: ModelContainer = {
        let v4Schema = Schema(V4Schema.models)
        let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "app group id")
        let databaseURL = containerURL?.appending(path: "**.sqlite")
        let configuration = ModelConfiguration("**",
                                               schema: v4Schema,
                                               url: databaseURL!,
                                               cloudKitDatabase: .private("**"))
        #if DEBUG
        do {
            try autoreleasepool {
                let desc = NSPersistentStoreDescription(url: configuration.url)
                let opts = NSPersistentCloudKitContainerOptions(containerIdentifier: configuration.cloudKitContainerIdentifier!)
                desc.cloudKitContainerOptions = opts
                
                desc.shouldAddStoreAsynchronously = false
                if let model = NSManagedObjectModel.makeManagedObjectModel(for: V4Schema.models) {
                    let container = NSPersistentCloudKitContainer(name: "**", managedObjectModel: model)
                    container.persistentStoreDescriptions = [desc]
                    container.loadPersistentStores(completionHandler: { _, err in
                        if let err {
                            Log.error(message: "Store open failed: \(err.localizedDescription)")
                        }
                    })
                    try container.initializeCloudKitSchema()
                    Log.debug(message: "Initialize Cloudkit Schema")
                    if let store = container.persistentStoreCoordinator.persistentStores.first {
                        try container.persistentStoreCoordinator.remove(store)
                    }
                }
            }
        } catch {
            Log.error(message: "Failed: \(error.localizedDescription)")
        }
        #endif
        
        return try! ModelContainer(for: v4Schema, migrationPlan: ModelMigrationPlan.self, configurations: configuration)
    }()

But when I run, I got error message.

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:]_block_invoke(3508): <NSCloudKitMirroringDelegate: 0x1036b1540> - Finished automatic export - ExportActivity - with result: <NSCloudKitMirroringResult: 0x1035da810> storeIdentifier: ***** success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134407 "Request '*****' was cancelled because the store was removed from the coordinator." UserInfo={NSLocalizedFailureReason=Request '****' was cancelled because the store was removed from the coordinator.}

I don't know why store was removed from the coordinator. Any have solutions?

Same problem here, any new insights about this?

I am encountering a similar problem as you, this time on MacOS Sonoma 14.5. Our team's code for integrating SwiftData and CloudKit is similar to yours and closely follows the official documentation from Apple regarding SwiftData and CloudKit: https://developer.apple.com/documentation/swiftdata/syncing-model-data-across-a-persons-devices

Our code worked on previous versions of Sonoma, but since updating to 14.5, we have consistently encountered the issue you are having, and it is infuriating. I am currently investigating why ours is failing, but it is frustrating to follow the documentation and still see our app crash.

SwiftData+Cloudkit Migration Failed
 
 
Q