Key data: handle the case when iCloud turns off / on

I use iCloud support in my Core Data application (only for iOS 7, but not released, iCloud support will run from day 1). I checked out a WWDC 2013 207 session about changes in iCloud, and I am very happy to see improvements (I had some previous experience with iCloud).

Everything works perfectly. However, I am not sure how to handle the case when the user enables or disables iCloud from the system settings - this leads to the creation of new files .sqlitein another directory and, consequently, to the loss of user data.

Here I use persistent store adding:

- (void) addPersistentStoreToCoordinator {

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setObject:@YES forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:@YES forKey:NSInferMappingModelAutomaticallyOption];

    NSURL *iCloud = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier: nil];

    if (iCloud) {
         [options setObject:@"ABC123~com~myapp~myapp" forKey:NSPersistentStoreUbiquitousContentNameKey];
    }

    NSError* error;

// the only difference in this call that makes the store an iCloud enabled store
// is the NSPersistentStoreUbiquitousContentNameKey in options.

    [persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType
                                          configuration:nil
                                                    URL:self.storeURL
                                                options:options
                                                  error:&error];
}

- (NSURL *)storeURL {

    NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
                                                                       inDomain:NSUserDomainMask
                                                              appropriateForURL:nil
                                                                         create:YES
                                                                          error:NULL];

    return [documentsDirectory URLByAppendingPathComponent:@"MyApp.sqlite"];
}

How to save data from Core Data when a user disables / disables iCloud preferences in settings?

( : , iCloud [OFF] > [ON] - , NSManagedObject iCloud ( ). ).

+4

All Articles