2

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

-com.apple.coredata.ubiquity.logLevel 3

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

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

注意:我已订阅通知。

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

1 回答 1

1

You need to set the object to the NSPersistentStoreCoordinator that you're using so that the notification knows which object to listen to. You've set it to nil in your code.

Example:

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

If you're posting the observer in a class that has access to the NSManagedObjectContext (but not the coordinator), you can simply extract the coordinator via self.managedObjectContext.persistendStoreCoordinator.

Hope this helps!

于 2014-03-24T16:17:50.590 回答