Reputation: 21
I can't save a record, which is already saved. recordToSave is a CKRecord download from the server. Is it possible to update it?
recordTosave.setValue("cat", forKey: "animal")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(recordToSave, completionHandler:{(record: CKRecord?, error: NSError?) in
if error == nil{
}else{
print(error.debugDescription)
}
})
Upvotes: 1
Views: 1177
Reputation: 101
You cannot insert record which is already existing in cloudkit. You can modify the record using CKModifyRecordsOperation. Fetch the record using record ID and then update through modify operation.
let recordIDToSave = CKRecordID(recordName: "recordID")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
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)
}
}
Upvotes: 6