注册以接收远程 CloudKit 更改的通知无效
Registering to receive notifications of remote CloudKit changes not working
我刚刚使用新的 iOS 13 NSPersistentCloudKitContainer
完成了 CoreData+CloudKit 的设置。它工作得非常好,因为我可以使用自动生成的 CoreData 类 进行 属性 访问和本地存储,并且 NSPersistentCloudKitContainer
自动同步设备之间的更改。我遇到的问题是收到远程更改通知。我查看了 Apple 文档,其中说明您告诉 NSPersistentCloudKitContainer
的 NSPersistentStoreDescription
您希望它发送通知,然后将其他对象注册为该通知的观察者。我已经这样做并添加了一个测试方法来显示何时检测到远程更改。测试方法生成的警报永远不会生成,但如果我终止应用程序并重新打开它,更改会立即出现。所以我相信远程更改正在同步并集成到本地 CoreData 存储中,但通知不起作用。我已将 Background Modes
权利添加到我的目标并选择了 Remote notification
模式。代码如下。如有任何帮助,我们将不胜感激!
设置发送通知的选项:
- (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:@"<redacted>"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
// ...
}
else {
// ...
[storeDescription setOption:@(YES) forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey];
// ...
}
}];
}
}
return _persistentContainer;
}
注册接收通知:
- (void)viewDidLoad {
[super viewDidLoad];
// ...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changes) name:NSPersistentStoreRemoteChangeNotification object:[CoreDataFunctions persistentContainer]];
}
响应变化的测试方法:
- (void)changes {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Changes received" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
无论您在何处访问应用程序的持久性 CloudKit 容器以获取 viewContext
,您都需要将 automaticallyMergesChangesFromParent
属性 设置为 true
。
lazy var managedContext: NSManagedObjectContext = {
self.storeContainer.viewContext.automaticallyMergesChangesFromParent = true
return self.storeContainer.viewContext
}()
进行这一单行更改将使应用程序(受 NSFetchedResultsController
支持)更新 UI 以响应远程数据更改…
你观察到的是持久容器而不是存储协调器,应该是这样的:
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(persistentStoreRemoteChangeNotification:) name:NSPersistentStoreRemoteChangeNotification object:_persistentContainer.persistentStoreCoordinator];
我刚刚使用新的 iOS 13 NSPersistentCloudKitContainer
完成了 CoreData+CloudKit 的设置。它工作得非常好,因为我可以使用自动生成的 CoreData 类 进行 属性 访问和本地存储,并且 NSPersistentCloudKitContainer
自动同步设备之间的更改。我遇到的问题是收到远程更改通知。我查看了 Apple 文档,其中说明您告诉 NSPersistentCloudKitContainer
的 NSPersistentStoreDescription
您希望它发送通知,然后将其他对象注册为该通知的观察者。我已经这样做并添加了一个测试方法来显示何时检测到远程更改。测试方法生成的警报永远不会生成,但如果我终止应用程序并重新打开它,更改会立即出现。所以我相信远程更改正在同步并集成到本地 CoreData 存储中,但通知不起作用。我已将 Background Modes
权利添加到我的目标并选择了 Remote notification
模式。代码如下。如有任何帮助,我们将不胜感激!
设置发送通知的选项:
- (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:@"<redacted>"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
// ...
}
else {
// ...
[storeDescription setOption:@(YES) forKey:NSPersistentStoreRemoteChangeNotificationPostOptionKey];
// ...
}
}];
}
}
return _persistentContainer;
}
注册接收通知:
- (void)viewDidLoad {
[super viewDidLoad];
// ...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changes) name:NSPersistentStoreRemoteChangeNotification object:[CoreDataFunctions persistentContainer]];
}
响应变化的测试方法:
- (void)changes {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Changes received" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
无论您在何处访问应用程序的持久性 CloudKit 容器以获取 viewContext
,您都需要将 automaticallyMergesChangesFromParent
属性 设置为 true
。
lazy var managedContext: NSManagedObjectContext = {
self.storeContainer.viewContext.automaticallyMergesChangesFromParent = true
return self.storeContainer.viewContext
}()
进行这一单行更改将使应用程序(受 NSFetchedResultsController
支持)更新 UI 以响应远程数据更改…
你观察到的是持久容器而不是存储协调器,应该是这样的:
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(persistentStoreRemoteChangeNotification:) name:NSPersistentStoreRemoteChangeNotification object:_persistentContainer.persistentStoreCoordinator];