can't delete

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

Replies

Hi,


I think I see what your issue is. I looked at the docs for `CKModifyRecordsOperation.modifyRecordsCompletionBlock`. At the end of the “Discussion” it says this:


While this block is executed after the completion of the modification of records, it is executed prior to the indexing of queries against those modified records. Therefore, if a query is executed in this completion block the results of that query may not include the changes made by this operation.


So, maybe try getting the count in `completionBlock` instead, which should run after `modifyRecordsCompletionBlock`.


Rob

Do you mean by 'completionBlock' the completion closure of the privateDatabase.perform(_:_:) method?

I think you are working on the problem that the printNumberOfRecords() method is printing the wrong number of records. That's not the problem I'm concerned with. I am concerned that the records are not deleted.

No, I mean the property of the CKModifyRecordsOperation. It's declared in the Operation (NSOperation) class. The inheritance is: CKModifyRecordsOperation <— CKDatabaseOperation <— CKOperation <— Operation.

I don't follow. Would you show me in my code what you mean?

Your other comment is right – that documentation I quoted explains why your call to `printNumberOfRecords` could give in accurate results, but it doesn’t explain why records aren’t deleted. That I don't know - sorry.