CKQueryOperation 中的 RequestRateLimited

RequestRateLimited in CKQueryOperation

如果 CKQueryOperation returns 和 RequestRateLimited 错误,是否应该将相同的 queryOperation 添加到 publicDatabase,还是应该根据收到的游标创建一个新的 queryOperation?如果发生 RequestRateLimited 错误,客户端是否收到光标?


@farktronix:

我实现得好吗,因为我得到一个错误(在模拟器中,在网络条件差的情况下)

-[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 ..
})

如果您收到 CKErrorRequestRateLimited 错误,则您不应收到新的查询游标。

任何时候您收到速率限制错误时,您都可以在 CKErrorRetryAfterKey 键下的 userInfo 字典中指定的时间过去后再次重试相同的操作。