CKQueryOperation错误:此操作受速率限制

时间:2015-04-15 21:50:10

标签: ios cloudkit ckquery

在Apple审核期间,只有一次使用error 1298: This operation has been rate limited下载记录时,客户端从CloudKit收到CKQueryOperation错误。我该如何解决这个问题?

这是代码,没什么特别的:

let query = CKQuery(recordType: "Movie", predicate: NSPredicate(format: "creationDate > %@", d!))
let qo = CKQueryOperation(query: query)
let fb: (CKRecord!) -> () = {record in

    temporaryContext.performBlockAndWait({

        let fr = NSFetchRequest(entityName: "Movie")
        fr.predicate = NSPredicate(format: "recordName = %@", record.recordID.recordName)

        let a = temporaryContext.executeFetchRequest(fr, error: nil) as! [Movie]
        if a.count == 0 {

            let m = NSEntityDescription.insertNewObjectForEntityForName("Movie", inManagedObjectContext: temporaryContext) as! Movie
            m.title = record.valueForKey("title") as! String
            m.image = (record.valueForKey("image") as! CKAsset).fileURL.description
            m.imageSize = Int32(record.valueForKey("imageSize") as! Int)
            m.recordName = record.recordID.recordName
        }
    })
}

let c: ()->() = {

    temporaryContext.performBlockAndWait({
        let success = temporaryContext.save(nil)
    })

    Utility.managedObjectContext().performBlockAndWait({

        let success = Utility.managedObjectContext().save(nil)
    })

    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "moviesDownloaded")
    NSUserDefaults.standardUserDefaults().synchronize()

    dispatch_semaphore_signal(self.sema)
}

let cb: (CKQueryCursor!, NSError!) -> () = {cursor, error in

    if error == nil {

        if cursor != nil {

            let qo2 = Utility.qo(cursor, recordFetchedBlock: fb, completion: c)
            publicDatabase.addOperation(qo2)

        } else {

            c()
        }

    } else {
        Utility.log("error 1298: \(error.localizedDescription)")

        dispatch_sync(dispatch_get_main_queue(), {
            self.status.backgroundColor = UIColor.orangeColor()
        })

        NSThread.sleepForTimeInterval(0.5)

        dispatch_semaphore_signal(self.sema)
    }
}

qo.recordFetchedBlock = fb
qo.queryCompletionBlock = cb
publicDatabase.addOperation(qo)

dispatch_semaphore_wait(self.sema, DISPATCH_TIME_FOREVER)

我尝试将整个代码放入如下的循环中:

for i in 1 ... 2 {
var rateLimited = false

...
if error == nil {

} else {

    NSThread.sleepForTimeInterval(3)
    rateLimited = true
}
...

if !rateLimited {

     break
}
}

你认为它会起作用吗?

2 个答案:

答案 0 :(得分:12)

如果您获得CKErrorRequestRateLimited,则错误的CKErrorRetryAfterKey字典中会出现userInfo个错误。在重试操作之前,您应至少等待一段时间。

等待睡眠是一个坏主意,因为它可能会导致应用程序意外挂起,特别是如果该代码在主线程上运行。使用dispatch_afterNSTimer重新发送您的操作。

答案 1 :(得分:3)

如果您未登录iCloud帐户,也会收到此错误。