在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://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
总结一下:
- 尝试对要保存的记录批处理执行 CKModifyRecordsOperation。
- 如果操作返回
CKErrorLimitExceeded
,将这批记录拆分为多个较小的批处理,并将它们作为多个 CKModifyRecordsOperations 提交。 (一个简单的拆分就是将批处理分成两半,但这取决于批处理中的新记录中是否有 CKReferences 等因素。) - 如果这些新的 CKModifyRecordsOperations 中的任何一个因
CKErrorLimitExceeded
而失败,则拆分它们的记录...等等。
关于ios - CloudKit批处理结果为 "Limit Exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874712/