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

StackOverflow https://stackoverflow.com/questions/21977191

Question

I'm trying to receive a message when the local store changes to the iCloud Store.

This is a critical event. So my use case is a new device receiving the iCloud Store after starting with an empty store. 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. 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:

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];
    }
Was it helpful?

Solution

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. 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.

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.

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).

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...

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/

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top