【发布时间】:2014-02-24 01:16:55
【问题描述】:
我正在尝试在本地存储更改为 iCloud 存储时接收消息。
这是一个关键事件。所以我的用例是一个新设备在从一个空商店开始后接收 iCloud 商店。我想通知视图更新接收到的内容。
我像这样初始化我的托管对象上下文:
[self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeURL
options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore", [NSNumber numberWithBool:YES]: NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES]:NSInferMappingModelAutomaticallyOption}
error:&error];
我的下一步是收到以下通知:
NSNotificationCenter *dc = [NSNotificationCenter defaultCenter];
[dc addObserver:self
selector:@selector(storesWillChange:)
name:NSPersistentStoreCoordinatorStoresWillChangeNotification
object:psc];
[dc addObserver:self
selector:@selector(storesDidChange:)
name:NSPersistentStoreCoordinatorStoresDidChangeNotification
object:psc];
[dc addObserver:self
selector:@selector(persistentStoreDidImportUbiquitousContentChanges:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:psc];
我认为在name:NSPersistentStoreCoordinatorStoresDidChangeNotification 中实现更新用户界面应该可以做到这一点。但不知何故,这似乎是我打算做的。
编辑: 通过已接受答案中的相关帖子,我可以解决我的问题
我检查通知用户字典
像这样:storesDidChange:
NSNumber *storeVal = [note.userInfo objectForKey:NSPersistentStoreUbiquitousTransitionTypeKey];
if (storeVal.integerValue == NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted) {
//you are now in sync with iCloud
NSLog(@"On iCloud Store now");
[self.delegate storeHasChanged];
}
【问题讨论】:
标签: ios objective-c core-data icloud