Objective c 核心数据&x2B;iCloud如何处理来自云的数据更新

Objective c 核心数据&x2B;iCloud如何处理来自云的数据更新,objective-c,core-data,icloud,Objective C,Core Data,Icloud,我试图找出如何处理来自云端的数据更新。 在Swift示例应用程序中,所有操作都是“从盒子中”完成的,但在Obj-C中,当应用程序从iCloud获得更新数据时,我无法更新接口。 下面是我用于实例化容器和上下文的代码: @synthesize persistentContainer = _persistentContainer; - (NSPersistentCloudKitContainer *)persistentContainer { // The persistent contai

我试图找出如何处理来自云端的数据更新。 在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正在更新,但看起来只有在应用程序转到后台并返回活动状态后才会更新。

据我所知,关键是对代码进行以下更改: 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正在更新,但看起来只有在应用程序进入后台并返回活动状态后才会更新。

我认为您需要的是
NSPersistentStoreRemoteChangeNotification
,而不是
NSPersistentStoreRemoteChangeNotificationPostOptionKey
。我尝试过,但不幸的是,它没有帮助。您能解释到底是什么不起作用吗?例如,是否调用了
updateTable
?您是否在项目中执行了此操作?是的,在项目设置中启用了推送通知。在后台模式下,还启用了远程通知。例如,我为表视图调用重载数据。我在视图上放置了一个按钮,如果我点击它,表将显示更新的数据,但我不知道如何在从iCloud更新核心数据时更新UI。我认为您需要
NSPersistentStoreRemoteChangeNotification
,而不是
NSPersistentStoreRemoteChangeNotificationPostOptionKey
。我尝试了,但不幸的是,这没用。你能解释一下什么不起作用吗?例如,是否调用了
updateTable
?您是否在项目中执行了此操作?是的,在项目设置中启用了推送通知。在后台模式下,还启用了远程通知。例如,我为表视图调用重载数据。我在视图上放了一个按钮,若我点击它,表格就会显示更新的数据,但我不知道当核心数据从iCloud更新时如何更新UI。