iosswiftcloudkitckrecord

How to delete a CKRecord


How do I delete a CKRecord from my database programmatically, on the client side.

for record in records {
    if recordNumber < 13 {
        let moodRecord:CKRecord = record as! CKRecord
        self.moodArray.append(moodRecord.objectForKey("Color") as! String)
    }
    else if recordNumber > 13 {
        // DELETE RECORD HERE
        record.delete(CKRecord)
        recordNumber--
    }
    recordNumber++
}

Solution

  • That's not how you delete records in CloudKit. Here's how I delete records in my app:

    let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: [record.recordID])
    operation.savePolicy = .AllKeys
    operation.modifyRecordsCompletionBlock = { added, deleted, error in
        if error != nil {
            println(error) // print error if any
        } else {
            // no errors, all set!
        }
    }
    CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation)