首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将已删除的CKRecord与CoreData NSManagedObject协调

将已删除的CKRecord与CoreData NSManagedObject协调
EN

Stack Overflow用户
提问于 2019-11-04 17:37:26
回答 2查看 59关注 0票数 0

我已经创建了CKQuerySubscriptions来监视CKRecords的远程插入、修改和删除。对于插入和修改的记录,这很有效,因为我可以在CloudKit中查询受影响的CKRecord,获取关联的NSManagedObject,然后从那里处理插入和修改。

对于已删除的CKRecord,这是一个问题,因为在触发通知时,该CKRecord已从CloudKit中删除。这意味着获取现在已删除的CKRecord的获取请求失败,因此我无法知道哪个NSManagedObject与已删除的CKRecord相关联。

我不知道我是不是走错了路,是否有更简单的方法来处理这一切!

EN

回答 2

Stack Overflow用户

发布于 2019-11-04 18:45:30

这是可行的,但感觉有点笨拙。一定有更简单的方法!但是如果没有,如果这段代码对其他人有用,如果你想要在任何未显示的帮助器方法中使用代码(例如,在+[CoreDataFunctions fetchRecordsForEntityType: withCloudIDs: completion:]方法中),请随意评论;

代码语言:javascript
复制
//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];
    });
}];
票数 0
EN

Stack Overflow用户

发布于 2020-04-22 20:21:57

我将订阅与远程通知和CKFetchRecordZoneChangesOperation一起使用。

如果调用了"application(didReceiveRemoteNotification:)“方法,则构建并激发"CKFetchRecordZoneChangesOperation”。

有几个完成处理程序。一个用于更新的记录(添加/修改),还有一个特殊的用于删除的记录。

这个处理程序称为"recordWithIDWasDeletedBlock“,为每个已删除的记录调用,提供已删除记录的recordID。有了这些信息,你应该能够处理你需要的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58690599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档