ChemDev
ChemDev

Reputation: 837

Not Able to Migrate Persistent Store Away from iCloud

I am trying to remove the iCloud sync capability from a NSPersistentStore in my app. The migration does not return any errors, however, the data from the iCloud-enabled persistent store disappears after the migration.

The following is my relevant code:

-(void)setUpCoreDataStackWithICloud
{
//Set up the model and context.
    self.model = [NSManagedObjectModel mergedModelFromBundles:nil];
    self.context = [[NSManagedObjectContext alloc]init];
    [self.context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

    if ([self isiCloudEnabledOnThisDevice]==YES) {
        [self setUpiCloud];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:[NSNumber numberWithBool:YES] forKey:@"lastLaunchOfAppiCloudWasEnabled"];

    }else self.psc = [self persistentStoreCoordinatorForUserWithoutiCloudEnabled];

    [self.context setPersistentStoreCoordinator:self.psc];
}
    -(NSPersistentStoreCoordinator*)persistentStoreCoordinatorForUserThatAlreadyMigratedToiCloud
{
    NSLog(@"persistentStoreCoordinatorForUserThatAlreadyMigratedToiCloud called");
    NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:self.model];

    NSError *error = nil;

    NSPersistentStore *store = [psc addPersistentStoreWithType:NSSQLiteStoreType
                                                 configuration:nil
                                                           URL:[self newStoreURL]
                                                       options:[self iCloudStoreOptions]
                                                         error:&error];
    if (!store) {
        NSLog(@"failed to add store with error %@",  [error localizedDescription]);
    }else NSLog(@"successfully added store for user that has already migrated to iCloud");


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

    NSError *migrationError = nil;

    NSPersistentStore *migratedStore = [psc migratePersistentStore:store toURL:[self testNewestStoreURL] options:migrateOptions withType:NSSQLiteStoreType error:&migrationError];

    if (migrationError) {
        NSLog(@"Migration from iCloud enabled persistent store was unsuccessful. %@", migrationError.localizedDescription);
    }else NSLog(@"The migration from the iCloud enabled persistent store was successful");

    NSLog(@"The migrated store is %@", migratedStore);

    return psc;
 }

-(NSURL*)testNewestStoreURL{
NSString *path = [self itemArchivePathWithAppendedString:@"test.data"];
NSURL *storeURL = [NSURL fileURLWithPath:path];
return storeURL;
}

-(NSURL*)newStoreURL
{
            NSString *path = [self itemArchivePathWithAppendedString:@"cloudstore.data"];
            NSURL *storeURL = [NSURL fileURLWithPath:path];
            return storeURL;
        }

Upvotes: 2

Views: 60

Answers (1)

ChemDev
ChemDev

Reputation: 837

The reason it won't work is because I am also doing a schema migration using a mapping model. iCloud doesn't support this according to the docs. Even though I am trying to get away from iCloud, it does not allow access to the data when there is a schema migration involved.

Upvotes: 1

Related Questions