简体   繁体   English

CloudKit:多个用户同时写入同一条记录

[英]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.我目前正在处理我一直在开发的应用程序的 CloudKit 错误。 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)?使用 CloudKit(在 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? CloudKit 会自动为我做这件事吗? 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!任何有关 CloudKit 打算如何让我处理此类问题的见解将不胜感激! Thanks!谢谢!

You have to handle CKRecord Conflicts in a way that suits your application.您必须以适合您的应用程序的方式处理 CKRecord 冲突。 One way to handle is Latest update wins.一种处理方法是最新更新获胜。 To make it working, you can have a field on CKRecord, say lastModified.为了让它工作,你可以在 CKRecord 上有一个字段,比如 lastModified。 You can have the savePolicy for CKModifyRecordsOperation set to ifServerRecordUnchanged When saving the record, Cloudkit throws you serverRecordChanged error.您可以将savePolicyCKModifyRecordsOperation设置为ifServerRecordUnchanged保存记录时,Cloudkit 会抛出serverRecordChanged错误。 With this error, you'll also have 3 versions of CKRecord.出现此错误时,您还将拥有 3 个版本的 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. 1.) 您尝试保存的记录的先前版本, 2.) 您尝试保存的记录的确切版本, 3.) 在您提交请求时服务器持有的版本。

By comparing lastModified field of the CKRecord, you can decide which CKRecord wins.通过比较 CKRecord 的lastModified字段,您可以决定哪个 CKRecord 获胜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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