Domanda

I have an iOS 7 app that already uses Core Data. I have used the new iOS 7 method of integrating iCloud into my app to sync items stored in core data by using the following code as an example:

https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/README.md

This works great, except that all of the original data on the device doesn't show up in the iCloud store. I keep hearing that I need to migrate the data - but I can't find any examples on how to do this properly. Does anyone know how to do this?

I keep getting pointed to using migratePersistentStore:toURL:options:withType:error:, but I don't see how I can use this...

È stato utile?

Soluzione

Here is a sample app with a iCloud control panel to move the store to or from iCloud. To move your existing store you need to open it with the existing options but make sure you use iOS7 options for the target store. Take a look at the sample apps code in OSCDStackManager and if you have specific questions then post them. http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

- (bool)moveStoreFileToICloud:(NSURL*)fileURL delete:(bool)shouldDelete backup:(bool)shouldBackup {
    FLOG(@" called");

    // Always make a backup of the local store before migrating to iCloud
    if (shouldBackup)
        [self backupLocalStore];

    NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

    // Open the existing local store using the original options
    id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:fileURL options:[self localStoreOptions] error:nil];

    if (!sourceStore) {

        FLOG(@" failed to add old store");
        return FALSE;
    } else {
        FLOG(@" Successfully added store to migrate");

        bool moveSuccess = NO;
        NSError *error;

        FLOG(@" About to migrate the store...");
        // Now migrate the store using the iCloud options
        id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self icloudStoreURL] options:[self icloudStoreOptions] withType:NSSQLiteStoreType error:&error];

        if (migrationSuccess) {
            moveSuccess = YES;
            FLOG(@"store successfully migrated");
            [self deregisterForStoreChanges];
            _persistentStoreCoordinator = nil;
            _managedObjectContext = nil;
            self.storeURL = [self icloudStoreURL];
            // Now delete the local file
            if (shouldDelete) {
                FLOG(@" deleting local store");
                [self deleteLocalStore];
            } else {
                FLOG(@" not deleting local store");
            }
            return TRUE;
        }
        else {
            FLOG(@"Failed to migrate store: %@, %@", error, error.userInfo);
            return FALSE;
        }

    }
    return FALSE;
}

Altri suggerimenti

You move your existing store to a different path, and then you call the migrate method with the toURL set to the path where you want your store to end up.

You need to pass the options in that include the ubiquity settings that an iCloud store needs to have set.

When the migration is finished, you should have two copies of the store: the non-iCloud one which you moved aside, and the new one with iCloud options set. You can now remove the old store if you like, and just setup your Core Data stack to use the iCloud store.

Take a look at some of the methods in this example. In particular, look at the ones beginning with 'migrate'. You should be able to work out what steps to take to migrate data to a new cloud store.

Core Data sync is hard to get right, especially when you start to get into migrating in data. It is worth looking at other Core Data sync options like Wasabi Sync and Ensembles. They handle migration and merging of data automatically. (Disclosure: I develop Ensembles)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top