我正在使用CKModifyRecordsOperation在CloudKit中批量创建大约8000条记录。但是,对于约5%的记录,我收到一个奇怪的错误:record to insert already exists
。经过彻底的调查和重新测试后,我无法弄清楚为什么会这样。需要考虑的注意事项:
[NSUUID UUID].UUIDString
在本地为每条记录创建一个recordName。在重新测试期间,我还通过创建一组recordNames并将其计数与要上载的总记录数进行比较,确认没有重复的recordNames。有关为何产生这些错误的想法吗?
代码:
- (void)uploadInitialRecordsToCloudKit:(NSArray *)recordsToUpload {
NSArray *recordsForThisBatch = recordsToUpload;
NSArray *remainingRecords = nil;
if ([recordsToUpload count] > 350) recordsForThisBatch = [recordsToUpload subarrayWithRange:NSMakeRange(0, 350)];
remainingRecords = [recordsToUpload subarrayWithRange:NSMakeRange([recordsForThisBatch count], [recordsToUpload count] -[recordsForThisBatch count])];
CKModifyRecordsOperation *modifyRecordsOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsForThisBatch recordIDsToDelete:nil];
modifyRecordsOperation.modifyRecordsCompletionBlock = ^(NSArray<CKRecord *> * _Nullable savedRecords, NSArray<CKRecordID *> * _Nullable deletedRecordIDs, NSError * _Nullable operationError) {
if (operationError) NSLog(@"Error during batch save to CK: %@\n\n%@", [operationError localizedDescription], operationError.userInfo);
else NSLog(@"Batch upload to CK successful");
if ([remainingRecords count] > 0) [self uploadInitialRecordsToCloudKit:remainingRecords];
};
[[CKContainer defaultContainer] publicCloudDatabase] addOperation:modifyRecordsOperation];
}
答案 0 :(得分:0)
这似乎是一个CloudKit问题,因为第三次测试没有任何错误。