SwiftData & CloudKit: Deduplication Logic

I followed these two resources and setup history tracking in SwiftData.

SwiftData History tracking: Track model changes with SwiftData history

For data deduplication: Sharing Core Data objects between iCloud users

In the sample app (CoreDataCloudKitShare), a uuid: UUID field is used for deduplication logic. It sorts the entities by their uuids, reserves the first one, and marks the others as deduplicated.

Would it be a viable solution to use a createdAt: Date field for the sort operation instead of a uuid field, or are dates inherently problematic?

Answered by DTS Engineer in 821011022

If the mentioned createdAt: date means the creation time of a Core Data object, it is local on a device, and multiple devices may create an object at the same time; if it means the creation time on the CloudKit server side, records created in a same batch (with a same CKModifyRecordsOperation) will have the same creation time. So overall, I think the creation time isn't as unique as a UUID.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

If the mentioned createdAt: date means the creation time of a Core Data object, it is local on a device, and multiple devices may create an object at the same time; if it means the creation time on the CloudKit server side, records created in a same batch (with a same CKModifyRecordsOperation) will have the same creation time. So overall, I think the creation time isn't as unique as a UUID.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you for your quick reply! Yes, I was referring to client-side date creation. I now understand that I need a solution with greater uniqueness. Thanks!

Irmak, May I ask how you are able to use both SwiftData and CloudKit in one app? I’m also trying to add CloudKit functionality into my SwiftData app, but have no idea where to begin.

SwiftData and CloudKit are two different frameworks, and so you are free to use both of them in your app. Note that CloudKit here means the CloudKit framework, not NSPersistentCloudKitContainer.

As of today, when you configure SwiftData to use CloudKit, as mentioned in here, SwiftData uses NSPersistentCloudKitContainer underneath to sync the data, and so you won't want to create your own NSPersistentCloudKitContainer instance for the purpose.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SwiftData & CloudKit: Deduplication Logic
 
 
Q