我的应用程序使用包含在 UIManagedDocument 中的核心数据数据库。当我尝试通过 iCloud 同步时,数据很少刷新。我通过将以下应用程序参数添加到我的方案来打开无处不在的日志。
-com.apple.coredata.ubiquity.logLevel 3
参数日志输出显示目标设备在源设备上进行更改后很快就能识别出更改,但未触发 NSPersistentStoreDidImportUbiquitousContentChangesNotification 通知。有时,通知会在看到更新后的很长一段时间内被触发,但通常不会。
但是,当我重新启动该应用程序时(在日志打印出一些关于更改的文本后的任何时间), NSPersistentStoreDidImportUbiquitousContentChangesNotification 通知立即被触发,导致数据刷新。
注意:我已订阅通知。
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updatedFromCloud:)
name: NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:nil];
最佳答案
您需要将对象设置为您正在使用的 NSPersistentStoreCoordinator,以便通知知道要监听哪个对象。您已在代码中将其设置为 nil。
例子:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(updatedFromCloud:)
name: NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:self.persistentStoreCoordinator];
如果您将观察者发布到可以访问 NSManagedObjectContext(但不能访问协调器)的类中,您可以通过 self.managedObjectContext.persistendStoreCoordinator 简单地提取协调器。
希望这对您有所帮助!
关于ios - iCloud NSPersistentStoreDidImportUbiquitousContentChangesNotification 仅在应用启动后立即调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702386/