SwiftData migration error: NSCloudKitMirroringDelegate are not reusable

Hello everyone,

I used SwiftData for v1 of an app and am now trying to make changes to the schema for v2. I created the v2 schema that adds a property to one of the models.

I need to populate the new property so I made a custom migration using didMigrate. However that doesn't seem to matter what I do in the migration because creating the ModelContainer throws an error before didMigrate ever gets called.

The error is:

Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.}

Higher up in the Xcode output I see things like this (in order):

Request 'D25A8CB8-7341-4FA8-B2F8-3DE2D35B5273' was cancelled because the store was removed from the coordinator.
BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered
CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process.

How can I know from this output what I am doing incorrectly?

Any idea what I should take a look at or try to do differently?

This is a simple app with three models and nothing fancy. The only change in the schema is to add a property. The new property is declared as optional and has an inverse that is also declared as optional.

Thanks for any insight!

A couple other things I noticed.

First, the crash occurs between the calls to willMigrate and didMigrate.

Second, if I try to run the app again after the crash then it runs fine, albeit with the new property being nil since didMigrate didn't execute, but this seems to suggest that the crash occurs downstream from the new schema version being applied to the database.

The error message indicates that your app had "another instance of this persistent store actively syncing with CloudKit," which is a wrong configuration when using SwiftData + CloudKit.

I am wondering if it was the migration process creating multiple persistent stores, or if it was your code unintentionally did that. The former will be a framework bug.

Do you have a minimal sample, with detailed steps, to reproduce the issue? If yes, I'd be interested in taking a look. I provided a code example demonstrating how to use SchemaMigrationPlan here, which you can use to create a sample by adding your model container configuration, if you would like to.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks Ziqiao, I will take a look at the code sample you pointed to and try to come up with a reduction that I can share. Either way I'll report back.

Trying different things with my project, I discovered that the problem only occurs when CloudKit syncing is enabled. Turning off syncing by passing cloudKitDatabase: .none to ModelConfiguration or by unchecking the CloudKit option in Signing & Capabilities makes the problem go away. But then of course iCloud syncing doesn't work. Makes sense considering the error message I get.

I was able to reproduce the problem easily with a new project based on the SwiftData Xcode template. I don't think it's doing anything out of the ordinary but I'm looking forward to find out.

The project can be found here: https://www.icloud.com/iclouddrive/0a5WkBaQRA1N-FOLGjKukV8Ag#MigrationSample

There are actually two projects, one for schema v1 and one for schema v2.

To reproduce the error I get:

  1. Open the MigrationSampleV1 project, build and run.
  2. In the running app, create some data.
  3. Open the MigrationSampleV2 project, build and run.

Expected result: The migration kicks in and the app opens.

Actual result: A crash occurs before the migration's didMigrate.

Oh here's a direct link to the zip file instead of a shared folder. https://www.icloud.com/iclouddrive/022QsLMXmVHAU2lAcuj4XuV4w#MigrationSample

Thanks for providing the project and steps. I tried them with my iPhone 16 Plus + iOS 18.4 beta 2 (22E5216h), and reproduced the issue.

Other than the error message you described, I saw other errors happening at the earlier stage. For the reference of other folks, I pasted the messages here:

CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1246): <NSCloudKitMirroringDelegate: 0x1361e8200>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x13624d900> (URL: file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store)
Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The mirroring delegate could not initialize because it's store was removed from the coordinator.}
...
BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered (com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246).
CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](610): <NSCloudKitMirroringDelegate: 0x1361e8500> - resetting internal state after error: Error Domain=NSCocoaErrorDomain Code=134422 "CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process." UserInfo={NSUnderlyingException=Illegal attempt to register a second handler for activity identifier com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246, NSURL=file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store, activityIdentifier=com.apple.coredata.cloudkit.activity.export.E2260BCD-9E87-4C0E-B3F3-68CA9843D246, NSLocalizedFailureReason=CloudKit setup failed because it couldn't register a handler for the export activity. There is another instance of this persistent store actively syncing with CloudKit in this process.}
...
CoreData: error: Store failed to load. <NSPersistentStoreDescription: 0x136028ea0> (type: SQLite, url: file:///var/mobile/Containers/Data/Application/C3C44E20-5618-4837-BB14-21CFF9E45799/Library/Application%20Support/default.store) with error = Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.} with userInfo {
NSLocalizedFailureReason = "Instances of NSCloudKitMirroringDelegate are not reusable and should have a lifecycle tied to a given instance of NSPersistentStore.";
}

I believe this is a system bug – It is actually quite similar to a known issue (r.127136484) that is supposed to be fixed. Now that the issue appears in today's latest beta, I’d suggest that you file a feedback report – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks Ziqiao. I filed the report. It's FB16712454.

I'm a bit stuck, I believe I'm experiencing this issue too although I'm not sure if it's exactly the same or not. I'm trying to test a migration from V2 to V3 in the simulator but the app always terminates before didMigrate with issues such as:

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.
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2313): <NSCloudKitMirroringDelegate: 0x600003d05d10> - Attempting recovery from error: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=The mirroring delegate could not initialize because it's store was removed from the coordinator.}
BUG IN CLIENT OF CLOUDKIT: Registering a handler for a CKScheduler activity identifier that has already been registered (com.apple.coredata.cloudkit.activity.export.8315617A-2590-4989-A1F0-0AACDDA4713E).

I'm not sure the first one is relevant, but the others are similar to those of the OP. I have attached the entire output here:

The test device is an iPhone 16 simulator running iOS 18.3. @DTS Engineer what is the recommended course of action for me? Do I need to wait for a fix, is it then an Xcode fix or in iOS? It seems I cannot perform migrations at all until this is fixed which seems like an issue that would affect many people?

Thanks! Will

@sroy I just faced exactly same error this morning, with a trivial migration like yours.

I just needed to add a new field with default value to a model, actually it can be migrated automatically but I needed to do some additional work so I created a migration plan.

And now it turned out there is a system bug with CloudKit that was supposed to be fixed!

I wonder so no one can do SwiftData migration now? let assume most people use iCloud sync.

Any news @DTS Engineer? Thanks!

@DTS Engineer I'm experiencing the exact same errors when attempting to perform a SwiftData custom migration with relatively simple models. Are there any updates you could share with us? Right now it seems like SwiftData isn't usable since this error is unavoidable when syncing to CloudKit, unless of course you never need to migrate, which unfortunately isn't realistic. Thanks!

Same error, i don't even have a migration, and all of sudden my project stopped working and the app launches and is stuck on first screen. If I disable by doing .cloudKitDatabase: .none. It will be able to run again. The issue is, I didn't even modify my SwiftData models between my last good build from 6 hours ago to this current build. I can still install the last good build from TestFlight and regain my cloudkit features, but i'm currently stuck with my development because of this issue.

Is there anyone see sroy feedback report above? I clicked and it said Feedback not found!!!

Thanks Ziqiao. I filed the report. It's FB16712454.

SwiftData migration error: NSCloudKitMirroringDelegate are not reusable
 
 
Q