Avoid Duplicate Records with CloudKit & CoreData

When my app starts it loads data (of vehicle models, manufacturers, ...) from JSON files into CoreData.  This content is static.

Some CoreData entities have fields that can be set by the user, for example an isFavorite boolean field.

How do I tell CloudKit that my CoreData objects are 'static' and must not be duplicated on other devices (that will also load it from JSON files).

In other words, how can I make sure that the CloudKit knows that the record created from JSON for vehicle model XYZ on one device is the same record that was created from JSON on any other device?

I'm using NSPersistentCloudKitContainer.

NSPersistentCloudKitContainer doesn’t support unique constraints today. To make sure that a record is unique across devices, you need to implement your own mechanism. In case you are interested in doing that, I have a sample project (I use it as my testbed) that may help:

It is a SwiftUI project that supports iOS and watchOS, and covers the following topics:

  1. Creating the CloudKit Schema for Apps
  2. Setting up the Core Data Stack
  3. Sharing a Core Data object
  4. Detecting Relevant Changes by Consuming Store Persistent History
  5. Removing Duplicate Data
  6. Implementing a Custom Sharing Flow

#4 and #5 are probably the most relevant to your question.

Feel free to provide your feedback here so I can improve the project and hopefully better help.

Updated link to the sample project: https://github.com/delawaremathguy/CoreDataCloudKitShare

Avoid Duplicate Records with CloudKit & CoreData
 
 
Q