TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: CloudKit. How to avoid duplicates

Forums > Swift

Hi!

I use CloudKit to save and restore records. How can I avoid devices storing records with same value? SwiftData has @Attribute(.unique) which works great. Is there something similar in CloudKit?

Thanks

   

If you start from scratch and don't have any data you can use a custom CKRecordID. https://developer.apple.com/documentation/cloudkit/ckrecord/id

Otherwise, you have to check yourself when creating and/updating a record.

If you want to make sure one or more of your custom fields in a record should be unique in your database you have to check yourself.

There are several possibilities to do so:

  • You can query for the corresponding field(s) before creating a record and prevent creating.
  • You can query for the corresponding field(s) before editing is saved.
  • You can prevent editing of the corresponding field(s).

1      

Thank you! I thought there would be an easier way. I'm thinking to let CK create records with same value, but when restoring filtering out the duplicates

   

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.