Ios 在CloudKit中保存修改后的数据
我一直在测试CloudKit,因为我希望在iOS8发布时使用它发布一个应用程序。使用以下代码保存数据似乎很简单:Ios 在CloudKit中保存修改后的数据,ios,objective-c,xcode,icloud,cloudkit,Ios,Objective C,Xcode,Icloud,Cloudkit,我一直在测试CloudKit,因为我希望在iOS8发布时使用它发布一个应用程序。使用以下代码保存数据似乎很简单: CKRecordID * recordID = [[CKRecordID alloc] initWithRecordName:@"basicRecord"]; CKRecord * record = [[CKRecord alloc] initWithRecordType:@"basicRecordType" recordID:recordID]; [record setValue:
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!");
}
}];
我没有收到任何错误。但是,如果我尝试再次运行代码,可能是因为我已将记录值更改为
[record setValue:@"newValue" forKey:@"defaultKey"];
我收到一个错误,这个错误引出了一个问题:如何保存修改过的数据。毕竟,这是将东西保存到云的一个基本部分。下面是错误,如果您有任何帮助,我们将不胜感激,请随时询问更多信息
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">
错误:
此错误可能是因为保存记录:
仅适用于新记录或比服务器上版本更新的记录:
仅当以前从未保存过记录或记录的版本比服务器上的版本更新时,此方法才会保存记录。无法使用此方法覆盖服务器上记录的较新版本
修改现有记录(或记录集)的建议方法是使用CKModifyRecordsOperation
集合和所需的savePolicy
来处理冲突:
修改记录的字段后,使用此类型的操作对象将这些更改保存到数据库中。(...)
保存记录时,savePolicy属性中的值确定在服务器上检测到冲突时如何继续
从以下文件:
新记录仅存在于内存中,直到您将其显式保存到iCloud
设置新值时[记录设置值:@“newValue”forKey:@“defaultKey”]代码>您已保存该记录,使其无效
您可以使用CKModifyRecordsOperation
,在大多数情况下,它可能更可取,但您不必这样做。只需使用一个新的CKRecord
获取数据,然后按说明将该记录馈送到saveRecord:
中。保存记录后,获取该记录,以便检索到的记录将具有Cloudkit添加的RecordID
然后,在同一个已获取的记录上,使用setValue更改要更改的数据
然后可以使用CFModifyRecordsOperation
在下面的示例中,cachedCKRecordsServiceCenter包含从cloudkit获取的记录,这些记录中包含cloudkit RecordID
//find this service center in the cached records
for (_,serviceCenter) in (theModel?.cachedCKRecordsServiceCenter.enumerated())! //is data for logged in Co ONLY with NO Co name attached
{
let name = serviceCenter["name"] as! String
returnValue = "Try Again"
if name == displayedRecordName
{
serviceCenter.setValue(displayedRecordName! + "_" + (theModel?.companyName)!, forKey: "name") //db values have Co name appended
serviceCenter.setValue(label2Text.text, forKey:"street1")
serviceCenter.setValue(label3Text.text, forKey:"street2")
serviceCenter.setValue(label4Text.text, forKey:"city")
serviceCenter.setValue(label5Text.text, forKey:"state")
serviceCenter.setValue(label6Text.text, forKey:"zip")
serviceCenter.setValue(label7Text.text, forKey:"phone")
serviceCenter.setValue(label8Text.text, forKey:"email")
serviceCenter.setValue(label9Text.text, forKey:"note")
let saveRecordsOperation = CKModifyRecordsOperation()
var ckRecordsArray = [CKRecord]()
// set values to ckRecordsArray
ckRecordsArray.append(serviceCenter)
saveRecordsOperation.recordsToSave = ckRecordsArray
saveRecordsOperation.savePolicy = .ifServerRecordUnchanged
appDelegate.locked = true
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error != nil {
// Really important to handle this here
////////print("ERROR: Unable to update Driver Location: Error= \(error)")
self.returnValue = "ERROR: Unable to update Driver Location: ERROR = \(error)"
self.appDelegate.locked=false
}
else
{
////print("Successfully updated Service Center")
self.appDelegate.locked=false
self.returnValue = "Successfully updated Service Center"
self.appDelegate.locked=false
//reget the data into the cach
self.theModel?.fetchServiceCenterFromCloudKit1()
}
}
CKContainer.default().publicCloudDatabase.add(saveRecordsOperation)
}
}