我创建了CKQuerySubscription
来监视CKRecord
的远程插入,修改和删除。对于插入和修改的记录,这很好用,因为我可以向CloudKit
查询受影响的CKRecord
,获取关联的NSManagedObject
,然后从那里处理插入和修改。
对于已删除的CKRecord
,这是一个问题,因为到通知被触发时,CKRecord
已从CloudKit
中删除。这意味着获取现在删除的CKRecord
的获取请求失败,因此我无法知道哪个NSManagedObject
与删除的CKRecord
相关联。
我不知道我是否会以所有错误的方式进行操作,是否有更简单的方法来处理所有这些问题!
答案 0 :(得分:0)
这可行,但感觉有点笨拙。一定有更简单的方法!但是,如果没有,如果此代码对其他任何人都有用,请随意注释是否要在未显示的任何辅助方法中使用该代码(例如,在+[CoreDataFunctions fetchRecordsForEntityType: withCloudIDs: completion:]
方法中);
//Array to hold all cloudIDs of existing NSManagedObject instances
NSMutableArray *cloudIDs = [NSMutableArray array];
//Populate cloudIDs array with the IDs of the existing NSManagedObject instances
for (NSManagedObject *item in self.items) {
NSUUID *cloudID = [item valueForKey:@"cloudID"];
[cloudIDs addObject:cloudID];
}
//Array to hold remaining NSManagedObject instances (i.e. the ones which were not deleted)
NSMutableArray *remainingItems = [NSMutableArray array];
//Fetch all remaining CKRecords (i.e. the ones which were not deleted
[CoreDataFunctions fetchRecordsForEntityType:[self managedObjectMonitoringClass] withCloudIDs:cloudIDs completion:^(NSArray<CKRecord *> *results) {
//For each local NSManagedObject instance
for (NSManagedObject *item in self.items) {
//The cloudID for the local NSManagedObject instance
NSString *localCloudID = [[item valueForKey:@"cloudID"] UUIDString];
//For each CKRecord in CloudKit
for (CKRecord *record in results) {
//The cloudID for the remote CKRecord object
NSString *remoteCloudID = [record valueForKey:@"CD_cloudID"];
//If the local and remote cloudIDs match, the local NSManagedObject entity represents a CKRecord which still exists in CloudKit
//Add the NSManagedObject entity to the remainingItems array
if ([remoteCloudID isEqualToString:localCloudID]) {
[remainingItems addObject:item];
break;
}
}
}
//Array to hold NSIndexPath objects to be removed from the collectionView
NSMutableArray *indexPaths = [NSMutableArray array];
//For each NSManagedObject stored locally
for (NSManagedObject *item in self.items) {
//If the remainingItems array does not contain this NSManagedObject, it has been deleted from CloudKit
//Create and indexPath for this item and add it to the array
if (![remainingItems containsObject:item]) {
NSInteger index = [self.items indexOfObject:item];
[indexPaths addObject:[NSIndexPath indexPathForItem:index inSection:0]];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[[self TBcollectionView] performBatchUpdates:^{
//Set the local items array to whatever is remaining in CloudKit
self.items = remainingItems;
//Delete the indexPaths for the items which were deleted
[[self TBcollectionView] deleteItemsAtIndexPaths:indexPaths];
} completion:nil];
});
}];
答案 1 :(得分:0)
我使用带有远程通知和CKFetchRecordZoneChangesOperation的订阅。
如果调用了“ application(didReceiveRemoteNotification :)”方法,则生成并触发“ CKFetchRecordZoneChangesOperation”。
有几个完成处理程序。一种是用于更新的记录(添加/修改),另一种是用于删除的记录。
此处理程序称为“ recordWithIDWasDeletedBlock”,并为已删除的每个单个记录调用,并提供已删除记录的recordID。有了这些信息,您应该能够处理所需的内容。