Romain
Romain

Reputation: 659

Cannot delete a record in CloudKit

I am using CloudKit for my iOS app. I created a method to delete record by ID.

My problem is that CKRecord are not deleted once I reinstall the app: when I reinstall the app and try to delete a CloudKit record, the method is executed successfully (error is nil) but the records are still in CloudKit.

PS: The deletion works fine as long as I delete records created by the same app instance.

   class func removeFavoriteEntryFromCloud(favoriteID id: String) {

    //Create the record
    let favoriteID = CKRecordID(recordName: id)

    privateDatabase.deleteRecordWithID(favoriteID) { (id: CKRecordID?, error: NSError?) -> Void in
        if error == nil {
            print("successfuly deleted record", id ?? "nil")
        }
    }

}

Here are my Roles in dashboard:

Here are my Roles in dashboard

Of course I use the same iCloud user account on the devices for my test.

Is that a bug in CloudKit Development Environment?

Upvotes: 4

Views: 2357

Answers (1)

pcnick13
pcnick13

Reputation: 114

 func deleteRecordWithID(_ recordID: CKRecordID, completion: ((_ recordID: CKRecordID?, _ error: Error?) -> Void)?) {

    publicDatabase.delete(withRecordID: recordID) { (recordID, error) in
        completion?(recordID, error)
    }
}

func deleteRecordsWithID(_ recordIDs: [CKRecordID], completion: ((_ records: [CKRecord]?, _ recordIDs: [CKRecordID]?, _ error: Error?) -> Void)?) {

    let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDs)
    operation.savePolicy = .ifServerRecordUnchanged

    operation.modifyRecordsCompletionBlock = completion

    publicDatabase.add(operation)
}

I use these two methods in my cloudKitManager and it deletes the record on my dashboard as soon as I refresh the browser. Similar to what you were doing, #1 get the recordID, #2 have a completion closure for the CKRecordID, #3 handle the error, and #4 complete with the recordID and error.

Checkout CKModifyRecordsOperation which can modify fields of a record. Its a really cool operation that also lets you use instances of this class to delete the records permanently from the database

Upvotes: 1

Related Questions