Search code examples
iosswifticloudcloudkit

CKQueryOperation visibility, cancellation and timeout possibilities


My application needs to run a CKQueryOperation using a CKDatabase. This requires network access which is not always available.

Once I add a CKQueryOperation to the CKDatabase that’s about where my control stops.

Is there a way I can:

  1. See of all pending operations specific to a CKDatabase.
  2. Cancel an operation once I’ve added it to the CKDatabase?
  3. Set a completion timeout so the operation doesn't stay pending if the network is preventing completion?

I know I can do this with CKDatabase performQuery however I would prefer to use operations if possible, thanks in advance for your help!


Solution

  • For timeout you can set configuration object on the CKQueryOperation:

    let config = CKOperation.Configuration()
    config.timeoutIntervalForRequest = 5
    config.timeoutIntervalForResource = 5
    
    // Config operation
    let operation = CKQueryOperation(query: query)
    operation.configuration = config
    operation.other_stuff..
    operation.queryCompletionBlock = { (_, error: Error?) in
            guard error == nil else {
                // Handle timeout error here.
                return
            }
            ...
     }
    

    Default timeout is 60 seconds.