4

I use NSPersistentCloudKitContainer to sync Core Data with Cloud Kit. To prepare for a new migration, I have created a new model version of the xcdatamodel and marked it as "current". I created a new entity and added a relationship from another entity. Nothing spectacular and suitable for a lightweight migration I thought.

Let's name this new entity: EntityNew

This is my code to initialize the NSPersistentCloudKitContainer:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentCloudKitContainer(name: "MyContainerName")


    container.loadPersistentStores(completionHandler: { _, error in
        guard let error = error as NSError? else { return }
        fatalError("###\(#function): Failed to load persistent stores:\(error)")
    })

    container.viewContext.automaticallyMergesChangesFromParent = true

    return container
}()

shouldMigrateStoreAutomatically and shouldInferMappingModelAutomatically are set to true by default.

Everything worked fine locally. No errors occurred during the migration.

The problems started when I created a new instance of EntityNew:

let newItem = EntityNew(context: context)   
newItem = "..."

saveContext()

newItem was created locally without any problems, but the iCloud Sync stopped working from this moment. The following error appeared in the console:

"<CKRecordID: 0x283fb1460; recordName=2E2209A1-F9F6-4DF2-960D-2C31F764ED05, zoneID=com.apple.coredata.cloudkit.zone:__defaultOwner__>" = "<CKError 0x2830a5950: \"Batch Request Failed\" (22/2024); server message = \"Atomic failure\"; uuid = ADA626F4-160E-49FE-A0BD-2198E5FBD09A; container ID = \"iCloud.[MyContainerID]\">"

"<CKRecordID: 0x283fb1a00; recordName=3145C837-D80D-47E0-B944-DBC6576A9B0A, zoneID=com.apple.coredata.cloudkit.zone:__defaultOwner__>" = "<CKError 0x2830a4000: \"Invalid Arguments\" (12/2006); server message = \"Cannot create or modify field 'CD_[Fieldname in EntityNew]' in record 'CD_[OtherEntityName]' in production schema\"; uuid = ADA626F4-160E-49FE-A0BD-2198E5FBD09A; container ID = \"iCloud.[ContainerID]\">";

"Cannot create or modify field 'CD_[Fieldname in EntityNew]' in record 'CD_[OtherEntityName]' in production schema"

Cloud Kit tries to modify the field CD_[Fieldname in EntityNew] (which is correct) on the record CD_[OtherEntityName], which is not the entity I created above! So Core Data tries to modify the wrong entity! This behavior does not happen for all fields (approx. 5 out of 10). I checked the local sqlite file of my iPhone but the local tables seems correct. The phenomenon can be observed in both, the Development and the Production icloud-container-environment. If I start with an empty database (which already contains the new entity, so no migration is necessary) the synchronization works.

What did I miss? Any ideas? Thank you!

StefanLdhl
  • 796
  • 7
  • 11
  • Could you clarify details about the new relationship? CloudKit has specific requirements for relationships, for example, they must be optional. See: https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/creating_a_core_data_model_for_cloudkit – Ch Ryder Aug 02 '20 at 09:08
  • The new relationship is marked as optional and has an inverse. The deletion rule is set to cascade and nullify (for the inverse). – StefanLdhl Aug 03 '20 at 14:13
  • That's good. Do the CloudKit schemas by any chance already have the same (legacy) fieldnames that the new relationships would create? – Ch Ryder Aug 03 '20 at 16:27
  • No.. the new relationship has a completely new name that does not collide with the existing fieldnames. – StefanLdhl Aug 05 '20 at 13:07
  • Did you call initializeCloudKitSchema() after adding the new entity? – Stamenkovski Aug 14 '20 at 15:48
  • When exactly do I have to call this method? At first start, after I have added the entity? – StefanLdhl Aug 29 '20 at 18:41
  • Did you solve this? I am having a very similar problem – Hamster May 09 '21 at 04:49
  • I have exact the same problem. – AlbertUI Dec 13 '21 at 01:57

0 Answers0