如何通过 CKModifyRecordsOperation 更新 CKRecord?
How to update CKRecord by CKModifyRecordsOperation?
我知道使用 ID 更新 CKRecord 只是使用 fetchRecordWithID 并使用如下所示的 CKModifyRecordsOperation。
publicData.fetchRecordWithID(recordIDToSave) { (record, error) in
if let recordToSave = record {
//Modify the record value here
recordToSave.setObject("value", forKey: "key")
let modifyRecords = CKModifyRecordsOperation(recordsToSave:[recordToSave], recordIDsToDelete: nil)
modifyRecords.savePolicy = CKRecordSavePolicy.AllKeys
modifyRecords.qualityOfService = NSQualityOfService.UserInitiated
modifyRecords.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error == nil {
print("Modified")
}else {
print(error)
}
}
publicData.addOperation(modifyRecords)
} else {
print(error.debugDescription)
}
}
但是我可以在没有 fetchRecord 的情况下更新记录吗?
我找到了解决方案。
只需将 CKModifyRecordsOperation 操作的保存策略设置为 changedKeys。
我知道使用 ID 更新 CKRecord 只是使用 fetchRecordWithID 并使用如下所示的 CKModifyRecordsOperation。
publicData.fetchRecordWithID(recordIDToSave) { (record, error) in
if let recordToSave = record {
//Modify the record value here
recordToSave.setObject("value", forKey: "key")
let modifyRecords = CKModifyRecordsOperation(recordsToSave:[recordToSave], recordIDsToDelete: nil)
modifyRecords.savePolicy = CKRecordSavePolicy.AllKeys
modifyRecords.qualityOfService = NSQualityOfService.UserInitiated
modifyRecords.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error == nil {
print("Modified")
}else {
print(error)
}
}
publicData.addOperation(modifyRecords)
} else {
print(error.debugDescription)
}
}
但是我可以在没有 fetchRecord 的情况下更新记录吗?
我找到了解决方案。
只需将 CKModifyRecordsOperation 操作的保存策略设置为 changedKeys。