Transfer iCloud Store to the local store and make sure that the data is accessible through each application launch

Based on the question here: Transfer iCloud data to local storage and stop responding iCloud by considering moving iCloud storage to your local store and make sure iCloud notifications are turned off, I have the following scenario that I got stuck.

Question

Right now, when the user launches the application on the first device, they are asked at the beginning whether they want to enable iCloud. If they select yes, the data is transferred to iCloud. If the user connects to the second device, they are asked if they want to use iCloud at the beginning, and if they select "yes", the data from the first device is synchronized.

If the user on the second device decides that he no longer wants to use iCloud, he can navigate and disable iCloud in the application for this device. This will transfer data from the iCloud store to the local store in the background. From the point of view of users, all their data is (regardless of whether it is stored in the cloud or on-premises, for the user it does not matter at the moment).

The problem is when I transfer my data through, it is successfully transferred to the local iCloud store and successfully stops listening to iCloud notifications. This aspect also works in the question I asked earlier ( Transferring iCloud data to local storage and stopping the iCloud response ).

, . , , .

, " " , :

- (void)migrateiCloudStoreToLocalStore {

NSLog(@"Migrate iCloudToLocalStore");
NSPersistentStore *store = self.persistentStoreCoordinator.persistentStores.lastObject;

//NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Envylope.sqlite"];
NSURL *storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
NSLog(@"Current Store URL (before iCloud to Local migration): %@", [storeURL description]);

NSDictionary *localStoreOptions = nil;
localStoreOptions = @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES,
                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                       NSInferMappingModelAutomaticallyOption : @YES};

NSPersistentStore *newStore = [self.persistentStoreCoordinator migratePersistentStore:store
                                                                                 toURL:storeURL
                                                                               options:localStoreOptions
                                                                              withType:NSSQLiteStoreType error:nil];

[self reloadStore:newStore];
}

- (void)reloadStore:(NSPersistentStore *)store {
NSLog(@"Reload Store");        
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Envylope.sqlite"];
NSDictionary *localStoreOptions = nil;
localStoreOptions = @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES,
                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                       NSInferMappingModelAutomaticallyOption : @YES};

if (store) {
    [self.persistentStoreCoordinator removePersistentStore:store error:nil];
}

[self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                              configuration:nil
                                                        URL:storeURL
                                                    options:localStoreOptions
                                                      error:nil];
storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
NSLog(@"Current Store URL (after iCloud to Local migration): %@", [storeURL description]);

NSLog(@"Done reloading");

[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"MigratedFromiCloudToLocal"];
[[NSUserDefaults standardUserDefaults]synchronize];

persistentStoreCoordinator.. , , , :

    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
        if (_persistentStoreCoordinator != nil) {
            return _persistentStoreCoordinator;
        }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Envylope.sqlite"];

    NSError *error = nil;

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

    if ((iCloud) && (![[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudOn"]) && (![[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]))
    {
        NSLog(@"In the persistent store, iCloud is enabled in the app device but not yet in the app and not on the latest version");

        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES};

    }
    else if ((iCloud) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudOn"]) && ([[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]))
    {
        NSLog(@"In the persistent store, iCloud is enabled, we're using iCloud from the Tutorial and we're on the latest version");
        NSURL *cloudURL = [self grabCloudPath:@"iCloud"];
        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES,
                                                           NSPersistentStoreUbiquitousContentURLKey: cloudURL, NSPersistentStoreUbiquitousContentNameKey: @"EnvyCloud"};


    }
    else if ((iCloud) && (![[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudOn"]) && ([[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]))
    {
        NSLog(@"In the persistent store, iCloud is enabled, we're on the latest version, btu we're not using icloud in the App");

        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES};

    }
    else
    {
        NSLog(@"iCloud is just not enabled");

        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES};

    }

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
    }
    return _persistentStoreCoordinator;
}

, , iCloud , iCloud , iCloud , iCloud , NSUserDefaults - . . 3- if true persistentStoreCoordinator, , , iCloud ( ), , , , .

?

.

+4
1

.

, , URL- .

, , URL-. , , . URL- iCloud, , URL-, ( ).

URL- - , , .

+2

All Articles