2
votes

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?

1
I found this sentence in the documentation "If you use just-in-time schema to populate a database with records, as described in Creating a Database Schema by Saving Records, you can reset the development environment between runs of your app." link Maybe the Development Environment is designed this way: I need to reset my zone during my tests. - Romain

1 Answers

0
votes
 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