Are CKRecords Guaranteed to be Added to an NSPersistentCloudKitContainer Store in creationDate order?

I am trying to deduplicate data created by NSPersistentCloudKitContainer in my app.

I have a universal app, with Share Extensions on both macOS and iOS. On each platform I share a store between the app and the extension with an App Group. The app and the extensions are both configured to sync to CloudKit. (This means local sharing is handled when offline, and a remote share extension will sync to CloudKit work when the main app is closed)

This configuration is causing duplicates to be generated. I believe this is because when the macOS app is open, both it and the macOS share extension will try and (almost simultaneously) sync a newly shared object, resulting in two copies in CloudKit.

On the macOS app, I can look through the persistent history and see the insertion 'author'. The first insertion is made by the extension "macOSShareExtension", the second is made by "NSCloudKitMirroringDelegate.import". I could easily make a choice to delete the second object.

However, at the same time, on the iOS app, I will get two insertions, both with the author "NSCloudKitMirroringDelegate.import".

I don't want to deduplicate only on the macOS app in this case. That would mean the the iOS app has duplicate objects until the deduplication propagates.

If I use CKRecord's creationDate to keep the first syncd Object, can I guarantee that if one Object has an associated CKRecord and the other doesn't, the one with out will subsequently gain a record with a later creationDate?

Should I be taking a different approach? Thank you.

Further to this. It appears that the error "[error] CoreData: Metadata Inconsistency: Missing metadata for record:" leads to these duplication errors. Perhaps this relates to a Share Extension being killed part way through the sync process? How can I avoid this issue?

My understanding of your problem (could be wrong):

You have a share extension and you have a parent Mac app. When you share data using the extension, the data gets duplicated

Questions

  1. Where are you storing the data (which entity) when data is share extension?

My approach (could be wrong)

  1. Share Extension should write to a separate entity that is not synced to iCloud
  2. Then sync the data from your share extension entity to your original entity
  3. You would have hooked your original entity to sync to iCloud so that should automatically take of syncing

Steps

  1. Add a new configuration called ShareExt (example name) to your CoreData data model (xcdatamodeld file )
  2. Add a new entity called MyEntity (example name)
  3. Add MyEntity to ShareExt configuration
  4. To persistentStoreDescriptions and add ShareExt (refer https://developer.apple.com/wwdc19/202)
  5. Sync from your MyEntity to your OriginalEntity (which you are using in the app) - refer https://developer.apple.com/documentation/coredata/linking_data_between_two_core_data_stores

Reference

Are CKRecords Guaranteed to be Added to an NSPersistentCloudKitContainer Store in creationDate order?
 
 
Q