【问题标题】:NSPersistentStoreDidImportUbiquitousContentChangesNotification not being notifiedNSPersistentStoreDidImportUbiquitousContentChangesNotification 未得到通知
【发布时间】:2012-07-21 18:48:41
【问题描述】:
如果另一个设备上的核心数据中的条目发生更改,NSLog 消息会显示它已注意到更改,但不会调用 NSPersistentStoreDidImportUbiquitousContentChangesNotification。直到在我的第一台设备上保存它才知道更新我的表格视图。
这是我的代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(iCloudUpdates:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:nil];
有人知道为什么这可能不起作用吗?
【问题讨论】:
标签:
iphone
objective-c
ios
core-data
icloud
【解决方案1】:
第一个选项:
如果您的问题是更新两台设备都有延迟,这是正常现象,Apple 不保证更新时间,但通常很快。
如果另一方面的问题是未调用 iCloudUpdates 方法,请确保签名正确,应该是:
-(void)iCloudUpdates:(NSNotification*)notification {
// do your stuff here
}
第二个选项:
在撰写本文时,iOS 5 的 iCloud 和 CoreData 存在很大的问题,我最近发布的应用程序没有 iCloud 支持。
如果您想知道发生了什么,请通过输入以下命令同时登录 CoreData 和 iCloud:
-com.apple.CoreData.SQLDebug 1
-com.apple.coredata.ubiquity.logLevel 3
在您的方案管理器中,在 run->arguments 选项卡下。
如果您在“目标”设备中看到一些奇怪的错误,则说明 iCloud 因被窃听而无法工作。
【解决方案2】:
我认为您的代码的问题在于,在“addObserver”中,您将对象设置为 nil。该对象应该是您的 persistentStoreCoordinator,如下所示。
__weak NSPersistentStoreCoordinator *psc = self.context.persistentStoreCoordinator;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(iCloudUpdates:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:psc];