Transfer iCloud data to local storage and stop iCloud from responding

I am so close to completing the implementation of iCloud and Core Data in my application. This app is only for iOS 7.

I give the user the opportunity in the application to say "Use iCloud On or Off." in uniform UISwitch. The application launches and asks the user whether they want to use iCloud or not, and subsequently this can be changed in the application.

I have data migrating from local storage (if it exists) to iCloud. One aspect I'm struggling with is transferring data from the iCloud Store to a local store.

Scenario: - User A has an iPhone with my application and some data - User A downloads the application on the iPad, chooses to use iCloud, but then at a later stage decides that they do not need synchronization on the iPad. So they turned off the iPad iCloud sync in the app.

At the moment, I understand that the user will expect that the data will still be there, but because of the backstage, now it is β€œLOCAL” instead of β€œiCloud” data, which means that recordings from another device will not be synchronized with this device and vice versa

Therefore, I am trying to achieve this.

Question

iCloud , , , iCloud - , , iCloud - " 1", "0", iPhone iPad, iCloud UISwitch .

, :

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

}

, , NSLog iCloud :

 Current Store URL (before iCloud to Local migration): file:///var/mobile/Applications/62CAFCEE-450E-410D-9D56-4918DD70EC07/Documents/CoreDataUbiquitySupport/mobile~98457667-A467-4095-BB7F-EF36EB2B0FE1/EnvyCloud/CD08912E-C135-4056-9557-6B47C84ECEF9/store/Envylope.sqlite

NSLog :

Current Store URL (after iCloud to Local migration): file:///var/mobile/Applications/62CAFCEE-450E-410D-9D56-4918DD70EC07/Documents/Envylope.sqlite

, . , , , iCloud ( , iCloud , - - iCloud). , iCloud. , , , . 1 0.

, , , , iCloud , , .

.

: . , , , , : iCloud Store , .

+2
2

: , @wottle . , iCloud :

- (void)removeCloudObservers {

[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreCoordinatorStoresWillChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:self.managedObjectContext.persistentStoreCoordinator];
}

, , iCloud ( ), singleton NSPersistentStoreCoordinator (removeUbiquitousContentAndPersistentStoreAtURL). iCloud ( yourStore), - :

- (void)removeCloudContainer:(NSPersistentStore *)yourStore {

NSError *error = nil;
if (![NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:yourStore.URL options:yourStore.options error:&error]) {
    NSLog(@"That didn't work so well, and here why: %@", error.localizedFailureReason);
}
}

, iCloud. , iCloud , .

UI, , , " iCloud" - , , , , iCloud .

+1

, , iCloud. - iCloud, -

NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(updateKVStoreItems:)
                                         name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                       object:store];

, , , iCloud Apple ( , iCloud).

[[NSNotificationCenter defaultCenter] removeObserver:self name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:nil];

, , , , iCloud Core Data , .

+1

All Articles