Search code examples
ioscloudkit

RequestRateLimited in CKQueryOperation


If a CKQueryOperation returns a RequestRateLimited error, should the same queryOperation added to publicDatabase, or should a new queryOperation created based on the cursor received? Does client receive cursor if RequestRateLimited error occurred?


@farktronix:

  • you shouldn't receive a new query cursor
  • you can retry the same operation again

am I implemented it well, because I get an error (in simulator, under poor internet condition)

-[NSOperationQueue addOperation:]: operation is finished and cannot be enqueued

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {

    // ..other things

    let qo = CKQueryOperation(query: query)
    let qcb: (CKQueryCursor!, NSError!) -> () = {cursor, error in

        if error == nil {

            //.. some code

        } else {

            if error.code == CKErrorCode.RequestRateLimited.rawValue {

                let retryAfter = error.userInfo![CKErrorRetryAfterKey] as! NSNumber

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(retryAfter.doubleValue * Double(NSEC_PER_SEC))), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

                    publicDatabase.addOperation(qo) // <- HERE is it ok? I get an error
                })
            } else {

                // .. some other code
            }
        }
    }

    qo.queryCompletionBlock = qcb
    publicDatabase.addOperation(qo)

    // .. other things ..
})

Solution

  • If you receive a CKErrorRequestRateLimited error then you shouldn't receive a new query cursor.

    Any time you receive a rate limited error you can retry the same operation again after the time specified in the userInfo dictionary under the CKErrorRetryAfterKey key has elapsed.