Search code examples
cloudkit

Checking if a CloudKit database operation is in progress


How can I check if a CKDatabase has an already in progress CKQueryOperation?

I'd like to fetch some CloudKit records but there is a chance I could submit the request more than once. To illustrate:

let container = CKContainer(identifier: "com.example.app")
let publicDB = container.publicCloudDatabase

// somehow determine if we have an already in progress operation here
if (inProgress) {
   return
 }

let query = CKQuery(recordType: "MyRecord", predicate: NSPredicate(value: true))
let operation CKQueryOperation(query: query)
// Omitting completion block for brevity
publicDB.add(operation)

Solution

  • Unfortunately there is no way to get information about operations on a Cloud Kit database's operation queue.

    One solution is to avoid the use of the CKDatabase add: method. Instead, create your own OperationQueue and add your database operations to this queue instead of using CKDatabase add:.

    Then you can check if your operation queue has any operations in it.

    Be sure you set the database operation's database property before adding the operation to your own queue.