我已经创建了CKQuerySubscriptions来监视CKRecords的远程插入、修改和删除。对于插入和修改的记录,这很有效,因为我可以在CloudKit中查询受影响的CKRecord,获取关联的NSManagedObject,然后从那里处理插入和修改。
对于已删除的CKRecord,这是一个问题,因为在触发通知时,该CKRecord已从CloudKit中删除。这意味着获取现在已删除的CKRecord的获取请求失败,因此我无法知道哪个NSManagedObject与已删除的CKRecord相关联。
我不知道我是不是走错了路,是否有更简单的方法来处理这一切!
发布于 2019-11-04 18:45:30
这是可行的,但感觉有点笨拙。一定有更简单的方法!但是如果没有,如果这段代码对其他人有用,如果你想要在任何未显示的帮助器方法中使用代码(例如,在+[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];
});
}];发布于 2020-04-22 20:21:57
我将订阅与远程通知和CKFetchRecordZoneChangesOperation一起使用。
如果调用了"application(didReceiveRemoteNotification:)“方法,则构建并激发"CKFetchRecordZoneChangesOperation”。
有几个完成处理程序。一个用于更新的记录(添加/修改),还有一个特殊的用于删除的记录。
这个处理程序称为"recordWithIDWasDeletedBlock“,为每个已删除的记录调用,提供已删除记录的recordID。有了这些信息,你应该能够处理你需要的东西。
https://stackoverflow.com/questions/58690599
复制相似问题