1

我正在研究两个 Mac 应用程序之间通过 iCloud 同步的核心数据。我大部分时间都在工作(基于http://timroadley.com/2012/04/03/core-data-in-icloud/) - 数据正在同步,但我从来没有收到我需要的内容更改通知将更改合并到我的本地上下文中。

在我的 AppDelegate 的 managedObjectContext 方法中:

NSManagedObjectContext * moc = [[NSManagedObjectContext alloc] 
  initWithConcurrencyType:NSMainQueueConcurrencyType];

[moc performBlockAndWait:
^{

  [moc setPersistentStoreCoordinator:coordinator];
  [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(mergeChangesFrom_iCloud:) 
    name:NSPersistentStoreDidImportUbiquitousContentChangesNotification 
    object:coordinator];

}];

我的观察者函数永远不会被调用:

- (void) mergeChangesFrom_iCloud:(NSNotification*) notification
{
  NSLog(@"Merging changes from iCloud");
  // everything past here is irrelevent - this log message never appears

数据存储肯定是同步的——删除应用程序 A 中的记录并强制应用程序 B 中重新加载表会导致核心数据故障失败,正如未合并更改所预期的那样。退出并重新启动应用程序 B 显示正确更新的数据。

addObserver 调用上的断点显示它正在被调用。中的断点mergeChangesFrom_iCloud表明它不是。

为什么协调员显然从未NSPersistentStoreDidImportUbiquitousContentChangesNotification像文档(和教程)声称的那样发送?我可以从哪里开始调试呢?

更新

谜团加深。使用此处显示的核心基础功能 - NSNotificationCenter 捕获和跟踪所有 NSNotifications - 我根本看不到NSPersistentStoreDidImportUbiquitousContentChangesNotification出现在飞过的大量通知中。

看起来它根本没有发布——在启动时发生初始加载后,我也没有看到任何其他与 Core Data 相关的通知。

4

0 回答 0