ios - CloudKit批处理结果为 "Limit Exceeded"

标签 ios iphone cloudkit

在CloudKit中,我尝试通过批处理来保存大量的记录。但是,我的应用程序因以下错误而崩溃:

Error pushing local data: <CKError 0x15a69e640: "Limit Exceeded" (27/1020); "Your request contains 561 items which is more than the maximum number of items in a single request (400)">

这是我的代码:

CKModifyRecordsOperation *modifyRecordsOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:localChanges recordIDsToDelete: localDeletions];
modifyRecordsOperation.savePolicy = CKRecordSaveAllKeys;
 
modifyRecordsOperation.modifyRecordsCompletionBlock = ^(NSArray *savedRecords, NSArray *deletedRecordIDs, NSError *error) {
    if (error) {
        NSLog(@"[%@] Error pushing local data: %@", self.class, error);
    }
};

[privateDatabase addOperation:modifyRecordsOperation];

如果我要获取一条记录,似乎都可以通过在 CKQueryOperation 中设置 resultsLimit 来获取。

https://stackoverflow.com/questions/24191999/cloudkit-count-records

https://stackoverflow.com/questions/26324643/cloudkit-your-request-contains-more-than-the-maximum-number-of-items-in-a-singl

https://forums.developer.apple.com/thread/11121

当我想在使用CKModifyRecordsOperation的批处理中保存大量记录时,有什么办法可以消除限制吗?

最佳答案

恐怕没有办法消除限制。而且您也不能指望它是 400 - 服务器可能决定拒绝任何大小的请求。

相反,按照 Apple 的建议处理 CKErrorLimitExceeded 错误:通过将操作重构为多个较小的批处理(即多个 CKModifyRecordsOperations)。

To ensure the speed of fetching and saving records, the server may reject large operations. When this occurs, a block reports the CKErrorLimitExceeded error. Your app should handle this error, and refactor the operation into multiple smaller batches.

来源:CKModifyRecordsOperation Class Reference

总结一下:

  1. 尝试对要保存的记录批处理执行 CKModifyRecordsOperation。
  2. 如果操作返回 CKErrorLimitExceeded,将这批记录拆分为多个较小的批处理,并将它们作为多个 CKModifyRecordsOperations 提交。 (一个简单的拆分就是将批处理分成两半,但这取决于批处理中的新记录中是否有 CKReferences 等因素。)
  3. 如果这些新的 CKModifyRecordsOperations 中的任何一个因 CKErrorLimitExceeded 而失败,则拆分它们的记录...等等。

关于ios - CloudKit批处理结果为 "Limit Exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874712/

相关文章:

ios - NSURLSession 3xx 重定向和完成处理程序

iphone - 入门 iPhone 开发问题

javascript - 使用 Node.js 验证 iOS 收据

swift - CloudKit数据管理

iphone - 以编程方式创建的按钮不会在 iOS 的自定义 View 中触发事件

ios - UIImagePickerController 在关闭时复制内容

iphone - 循环声音在循环结束时有咔嗒声

ios - 从 iOS 7 开始不推荐使用坐标

ios - 通过 CloudKit 打开共享文档时出错

ios - UITableView 不会更新数据