Core Data + iCloud 如何处理来自云端的数据更新

Core Data + iCloud how to handle data update from cloud

我想弄清楚如何处理来自云端的数据更新。 在 Swift 示例应用程序中,所有工作都“开箱即用”,但在 Obj-C 中,当应用程序从 iCloud 获取更新数据时,我无法更新界面。 在我实例化容器和上下文的代码下面:

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentCloudKitContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentCloudKitContainer alloc] initWithName:@"SampleCloudObjC"];
            NSPersistentStoreDescription *description = [_persistentContainer.persistentStoreDescriptions firstObject];
            [description setOption:@(true) forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    context.automaticallyMergesChangesFromParent = YES;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

我在这里尝试处理通知:

- (void)viewDidLoad {
    [super viewDidLoad];
    taskList = [Task fetchAll];
    [[NSNotificationCenter defaultCenter] 
        addObserver:self 
        selector:@selector(updateTable)     
        name:NSPersistentStoreRemoteChangeNotificationPostOptionKey 
        object:nil];
}

- (IBAction)updateTable {
    taskList = [Task fetchAll];
    [self.tableView reloadData];
}

据我了解,关键是对代码进行后续更改: AppDelegate:

...
NSPersistentStoreDescription *description = [_persistentContainer.persistentStoreDescriptions firstObject];
            [description setOption:@(true) forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
...

控制器:

...
[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(remoteHandler:)
     name:NSPersistentStoreRemoteChangeNotification
     object:nil];
...

现在 UI 正在更新,但看起来只有在应用程序进入后台并恢复活动状态后才会更新。