在CloudKit中保存修改后的数据 [英] Saving Modified Data in CloudKit

查看:284
本文介绍了在CloudKit中保存修改后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在测试CloudKit,因为我希望在发布iOS8时使用它来发布应用程序。使用下面的代码保存数据似乎很简单:

I have been testing out CloudKit as i wish to release an app using it when the release of iOS8 occurs. It seems simple enough to save data using the code below:

CKRecordID * recordID = [[CKRecordID alloc] initWithRecordName:@"basicRecord"];
CKRecord * record = [[CKRecord alloc] initWithRecordType:@"basicRecordType" recordID:recordID];
[record setValue:@"defaultValue" forKey:@"defaultKey"];
CKDatabase *database = [[CKContainer defaultContainer] publicCloudDatabase];
[database saveRecord:record completionHandler:^(CKRecord *record, NSError *error) {

    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Record Saved!");
    }
}];

我没有收到任何错误。但是,如果我再次尝试运行代码,可能是因为我已将记录值更改为

and I receive no errors from this. However, if i try to run the code again, maybe because i have changed the record value to

[record setValue:@"newValue" forKey:@"defaultKey"];

我收到错误提出问题,如何保存修改过的数据。毕竟,这是将事物保存到云端的基本部分。错误如下,任何帮助将不胜感激,请不要犹豫,询问更多信息。

I receive an error which begs the question, how do i go about saving a modified piece of data. After all, this is a fundamental part of saving things to the cloud. The error is below and any help would be greatly appreciated, don't hesitate to ask for further information.

Error: <CKError 0x17024afb0: "Server Record Changed" (14/2017); "Error saving record <CKRecordID: 0x144684a80; basicRecord:(_defaultZone:__defaultOwner__)> to server: (null)"; uuid = 182C497F-966C-418A-9E6A-5563BA6CC6CD; container ID = "iCloud.com.yourcompany.CloudKit">


推荐答案

此错误可能是因为 saveRecord:仅适用于比服务器上的版本更新的新记录或记录:

This error is probably because saveRecord: works only for new records or records that are newer than the version on the server:


此方法仅在以前从未保存过记录或者如果它比服务器上的版本更新时保存记录。您无法使用此方法覆盖服务器上记录的较新版本。 CKDatabase docs

修改现有记录(或记录集)的推荐方法是使用 CKModifyRecordsOperation 设置所需的 savePolicy 来处理冲突:

The recommended approach to modify an existing record (or set of records) is to use a CKModifyRecordsOperation set with the desired savePolicy to deal with conflicts:


修改记录的字段后,使用此类操作对象将这些更改保存到数据库。 (...)
保存记录时,savePolicy属性中的值确定在服务器上检测到冲突时如何继续。 CKModifyRecordsOperation docs

这篇关于在CloudKit中保存修改后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆