【问题标题】:iCloud NSPersistentStoreDidImportUbiquitousContentChangesNotification only called right after app launchiCloud NSPersistentStoreDidImportUbiquitousContentChangesNotification 仅在应用启动后立即调用
【发布时间】:2012-09-24 00:29:36
【问题描述】:

我的应用使用包含在 UIManagedDocument 中的核心数据数据库。当我尝试通过 iCloud 同步时,数据很少被刷新。我已通过将以下 app 参数添加到我的方案中来打开普遍性日志。

-com.apple.coredata.ubiquity.logLevel 3

参数日志输出显示目标设备在源设备上进行更改后很快就会识别出更改,但不会触发NSPersistentStoreDidImportUbiquitousContentChangesNotification 通知。有时,通知会在看到更新后的很长一段时间内触发,但通常不会。

但是,当我重新启动应用程序时(在日志打印一些有关更改的文本后的任何时间), NSPersistentStoreDidImportUbiquitousContentChangesNotification 通知会立即触发,导致数据刷新。

注意:我已订阅通知。

[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(updatedFromCloud:)
                                            name: NSPersistentStoreDidImportUbiquitousContentChangesNotification
                                          object:nil];

【问题讨论】:

  • 我有完全相同的问题,我将持久存储协调器作为对象参数传递。你找到解决办法了吗?

标签: ios core-data icloud nsnotificationcenter uimanageddocument


【解决方案1】:

您需要将对象设置为您正在使用的 NSPersistentStoreCoordinator,以便通知知道要收听哪个对象。您已在代码中将其设置为 nil。

例子:

[[NSNotificationCenter defaultCenter]addObserver:self
                                    selector:@selector(updatedFromCloud:)
                                        name: NSPersistentStoreDidImportUbiquitousContentChangesNotification
                                      object:self.persistentStoreCoordinator];

如果您将观察者发布到可以访问 NSManagedObjectContext(但不是协调器)的类中,您可以简单地通过 self.managedObjectContext.persistendStoreCoordinator 提取协调器。

希望这会有所帮助!

【讨论】:

  • 我有同样的问题,我将持久存储协调器(只有 1 个)作为对象参数传递。所以我认为这是由其他原因引起的。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2022-07-06
  • 2023-03-03
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
相关资源
最近更新 更多