I have code to batch delete records from the private database in CloudKit, but it's not working. (I also noticed that the records I saved are not remaining.)
It looks like some of the records that the code is supposed to delete is actually deleted, but some are not. When I run the code again, there exists records still, but about one less the number as before.
I think the most important thing about this problem is that IT OCCURS WHEN I USE AN APPLE ID THAT IS NOT MY DEVELOPER ACCOUNT.
Here is my code:
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let splitViewController = window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
splitViewController.delegate = self
deleteRecords()
return true
}
}
let privateDatabase = CKContainer.default().privateCloudDatabase
func deleteRecords() {
print("deleteRecords()")
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeAffirmation, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
(records: [CKRecord]?, error: Error?) in
if error != nil {
print(error as Any)
} else {
if let records = records {
print("records.count=", records.count)
let recordIDsToDelete = records.map { $0.recordID }
print("recordIDsToDelete:")
print(recordIDsToDelete)
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDsToDelete)
operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error == nil {
print("Batch delete records!")
print("number of records deleted:", deletedRecordIDs?.count as Any)
printNumberOfRecords()
} else {
print(error as Any)
}
}
privateDatabase.add(operation)
}
}
}
}
func printNumberOfRecords() {
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeAffirmation, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
(records: [CKRecord]?, error: Error?) in
if error != nil {
print(error as Any)
} else {
if let records = records {
print("Number of records in CloudKit=", records.count)
}
}
}
}
Here is the output in the debug window from the first run of the code:
deleteRecords()
records.count= 93
recordIDsToDelete:
[<CKRecordID: 0x280bbcb00; recordName=B33A3F23-23D3-44C6-AEBC-86DD718DBB62, zoneID=_defaultZone:__defaultOwner__>, ...]
Batch delete records!
number of records deleted: Optional(93)
Number of records in CloudKit= 67
Here is the output in the debug window from the second run of the code:
deleteRecords()
records.count= 92
recordIDsToDelete:
[<CKRecordID: 0x280080d00; recordName=BBA5B236-A036-4AC9-82E1-165D3B003E23, zoneID=_defaultZone:__defaultOwner__>, ...]
Batch delete records!
number of records deleted: Optional(92)
Number of records in CloudKit= 52