繁体   English   中英

iCloud和CoreData:更改为iCloud Store时的通知(首先使用现有iCloud数据启动)

[英]iCloud & CoreData: Notification when changed to iCloud Store (First Launch with existing iCloud Data)

当本地存储更改为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来实现更新ui应该可以解决。 但是,某种程度上看来,这就是我打算要做的。

编辑:在接受的答案中的相关文章,我可以解决我的问题

我在检查用户通知

像这样: 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];
    }

有关处理核心数据存储更改事件的描述,请参见下面的链接。 请注意,无论商店的状态如何,第一个storeDidChange通知都是相同的。 但是,如果这是您第一次创建商店,并且已经有一个iCloud商店,那么在现有iCloud商店初始导入完成后,您将收到另一个storeDidChange通知。

问题是,除非您知道自己正在创建新商店并且iCloud中已经存在商店,否则您在发生情况之前不知道情况如何。

不幸的是,正如我在解释中指出的那样,本地存储和iCloud存储之间没有真正的切换-但是Core Data确实以某种方式导入了sideLoad存储,这时您会获得过渡类型4通知(第二个storeDidChange)。

另请注意,如果您的商店需要升级到新的模型版本,那么您还会收到一大堆的storeDidChange通知...

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/handling-icloud-帐户转换/

您可能还想在相同的链接http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-上查看示例应用程序的工作方式。 核心数据的应用程序与- icloud的集成/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM