How to update CKRecord by CKModifyRecordsOperation?

816 views Asked by At

I know that update CKRecord With ID is just use fetchRecordWithID and use CKModifyRecordsOperation like below.

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)
    }
}

But Can I update record without fetchRecord?

1

There are 1 answers

0
최연석 On BEST ANSWER

I found the solution.

Just set CKModifyRecordsOperation operation's save policy to changedKeys.