简体   繁体   中英

CloudKit: multiple users writing to same record at same time

I am currently working on handling CloudKit errors for an app I've been developing. One major issue I need to handle is this: multiple users writing to the same record at the same time.

What is the best way to handle this issue with CloudKit (in Swift)? Do I need to add some sort of lock on the record so that only one user can edit at a time? Does CloudKit do this for me automatically? If so, should I re-try the operation after some interval of time? Do I need a queue to hold tasks that are waiting to be done on that record?

Any insight into how CloudKit intends for me to handle these sorts of issues would be much appreciated! Thanks!

You have to handle CKRecord Conflicts in a way that suits your application. One way to handle is Latest update wins. To make it working, you can have a field on CKRecord, say lastModified. You can have the savePolicy for CKModifyRecordsOperation set to ifServerRecordUnchanged When saving the record, Cloudkit throws you serverRecordChanged error. With this error, you'll also have 3 versions of CKRecord. 1.) The prior version of the record you tried to save, 2.) The exact version of the record you tried to save, 3.) The version held by the server at the time you submitted the request.

By comparing lastModified field of the CKRecord, you can decide which CKRecord wins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM