简体   繁体   English

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

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

I'm trying to receive a message when the local store changes to the iCloud Store. 当本地存储更改为iCloud存储时,我正在尝试接收消息。

This is a critical event. 这是关键事件。 So my use case is a new device receiving the iCloud Store after starting with an empty store. 所以我的用例是一个新设备,从一个空的存储开始,然后接收iCloud存储。 I would like to notify the view to update with the received content. 我想通知视图以接收到的内容进行更新。

I initialize my managed object context like this: 我这样初始化托管对象上下文:

[self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                   configuration:nil
                                                                             URL:self.storeURL
                                                                         options:@{ NSPersistentStoreUbiquitousContentNameKey : @"iCloudStore", [NSNumber numberWithBool:YES]: NSMigratePersistentStoresAutomaticallyOption,
                                                                                    [NSNumber numberWithBool:YES]:NSInferMappingModelAutomaticallyOption}
                                                                           error:&error];

My next step is to get the following notifications: 我的下一步是获取以下通知:

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];

I thought that implementing the update ui in the name:NSPersistentStoreCoordinatorStoresDidChangeNotification should do the thing. 我认为以name:NSPersistentStoreCoordinatorStoresDidChangeNotification来实现更新ui应该可以解决。 But somehow it appears that this is what I intended to do. 但是,某种程度上看来,这就是我打算要做的。

Edit: With the related post in the accepted answer I could solve my issue 编辑:在接受的答案中的相关文章,我可以解决我的问题

I check the notifications userdict in 我在检查用户通知

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

See link below for a description of handling Core Data store change events. 有关处理核心数据存储更改事件的描述,请参见下面的链接。 NOTE that the first storesDidChange notification is identical regardless of the state of your store. 请注意,无论商店的状态如何,第一个storeDidChange通知都是相同的。 However if this is the first time you are creating the store AND there is an iCloud store already you will get another storesDidChange notification once the existing iCloud store initial import has been completed. 但是,如果这是您第一次创建商店,并且已经有一个iCloud商店,那么在现有iCloud商店初始导入完成后,您将收到另一个storeDidChange通知。

The problem is you don't know what the situation is before it happens UNLESS you know you are creating a new store and a store already exists in iCloud. 问题是,除非您知道自己正在创建新商店并且iCloud中已经存在商店,否则您在发生情况之前不知道情况如何。

Sadly as noted in my explanation there is no real switch between a local store and an iCloud store - however Core Data does somehow import the sideLoad store at which point you get the transition type 4 notification (second storesDidChange). 不幸的是,正如我在解释中指出的那样,本地存储和iCloud存储之间没有真正的切换-但是Core Data确实以某种方式导入了sideLoad存储,这时您会获得过渡类型4通知(第二个storeDidChange)。

Also be aware that if your store needed to be upgraded to a new model version you also get a whole bunch of storesDidChange notifications... 另请注意,如果您的商店需要升级到新的模型版本,那么您还会收到一大堆的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-account-transitions/ http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/handling-icloud-帐户转换/

You may also want to check out how the sample apps do what you are trying to do at the same link http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ 您可能还想在相同的链接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