Search code examples
iosswiftnsoperationcloudkit

CKModifyRecordsOperation not deleting records


Following a query, I collected an array of CKRecordID objects, which I wished to delete from my database. It actually contains the ID for every object in the database right now, so it's easy to see when there are entries there.

I first tried to delete these through an operation, like so:

let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDsToDelete)
operation.modifyRecordsCompletionBlock = {
    (savedRecords: [CKRecord]?, deletedRecordIDs: [CKRecordID]?, error: NSError?) in
    print("complete! deleted record IDs: \(deletedRecordIDs)")
}

self.operationQueue.addOperation(operation)

But despite getting the callback listing all the recordIDs I expected, upon checking on the dashboard all of the items still remained.

So I tried this via different means, through a block, which I'd rather avoid since it doesn't put the work into an operation:

for recordID in recordIDsToDelete {
    publicDB.deleteRecordWithID(recordID, completionHandler: {
        (recordID: CKRecordID?, error: NSError?) -> Void in
        print("deleted record id: \(recordID)")
        print("error: \(error)")
    })
}

This works. I can of course make my own operation from that code, but it seems like I'm missing something as to why the already-existing operation doesn't work.


Solution

  • Answering my own question here...

    CKModifyRecordsOperation inherits from CKDatabaseOperation, which has a database property which defaults to the private database, so those operations were being performed on a different database. I simply set the database to be the public one, and it all works:

    operation.database = CKContainer.defaultContainer().publicCloudDatabase