How to seed data with CloudKit?

I need to create some records in CloudKit for each user when they start an app.


I can't just write a seed function that create records. Because when the user starts the app in two devices, they will each write their own seed record.


What I want instead is for the first device to write to CloudKit gets to create the record. And then second device will simply update the values of those records no recreate them.


How can I achieve this?

One simple approach is to first query for the existence of the record. If you receive a successful response of null, then write the record. This leaves open a possibility of a race condition if two devices are both doing this simultaneously, but in that case you can always use the earliest of the returned records.


A second and equally simple approach would be to write the record in all cases, but only query for the record with the earliest creationDate, permitting the first written record to be used by all devices.

How to seed data with CloudKit?
 
 
Q