Why? "CloudKit integration does not support unique constraints"

Hi,

On iOS 14 beta 1-3 I was happily using a constraint on several entities in NSPersistentCloudKitContainer.
The part of the app using this was essentially an RSS reader - two different devices were independently fetching news and no duplicates due to the constraints.

Now on iOS14 beta 4 I'm receiving "CloudKit integration does not support unique constraints", which means I've had to remove the constraints..
Now the app is subject to CloudKit sync vs RSS import timing issues.. if CloudKit syncs after the RSS import then there are "duplicate" items.

What's the "workaround" for this please? It really should just work.

Thanks,

Andy
Answered by Engineer in 625717022
Unfortunately this was never supported, and fails in byzantine ways, so it produces a clear error up front now.

You can use the persistent history to de-dupe whenever you import new changes. You can be informed of this from observing the NSPersistentStoreCoordinator posting NSManagedObjectContextDidSaveObjectIDsNotification, or the new NSPersistentCloudKitContainerEvent APIs with NSPersistentCloudKitContainerEventChangedNotification

Alternatively, you can use two persistent stores, one local and one CloudKit. You'll put the entities in different Configurations and add them to the same NSPersistentStoreCoordinator. The entities in the local configuration can use any Core Data features including unique constraints. You'd use persistent history and the notifications to drive importing and exporting between the two store files. This is more customizable but obviously a considerable amount of work.

I am experiencing the same problem in my app. Any other workarounds other people have found without having to remove the constraints?
Please follow this document. They already have mention regarding entities constraint aren't supported with CloudKit model.

https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/creating_a_core_data_model_for_cloudkit
NSPersistentCloudKitContainer never supported unique constraints. In iOS 14 we made this a lot more obvious.
Accepted Answer
Unfortunately this was never supported, and fails in byzantine ways, so it produces a clear error up front now.

You can use the persistent history to de-dupe whenever you import new changes. You can be informed of this from observing the NSPersistentStoreCoordinator posting NSManagedObjectContextDidSaveObjectIDsNotification, or the new NSPersistentCloudKitContainerEvent APIs with NSPersistentCloudKitContainerEventChangedNotification

Alternatively, you can use two persistent stores, one local and one CloudKit. You'll put the entities in different Configurations and add them to the same NSPersistentStoreCoordinator. The entities in the local configuration can use any Core Data features including unique constraints. You'd use persistent history and the notifications to drive importing and exporting between the two store files. This is more customizable but obviously a considerable amount of work.

May I complain why this behavior was undocumented on iOS 13?

Alternatively, you can use two persistent stores, one local and one CloudKit. You'll put the entities in different Configurations and add them to the same NSPersistentStoreCoordinator. The entities in the local configuration can use any Core Data features including unique constraints. You'd use persistent history and the notifications to drive importing and exporting between the two store files. This is more customizable but obviously a considerable amount of work.

Do you have some example for this?
Why? "CloudKit integration does not support unique constraints"
 
 
Q