alexandresoli
alexandresoli

Reputation: 928

Migrate Core Data App to iCloud

This is my scenario:

My app use core data and i'm adding iCloud support, but i'm having problems trying to migrate users data to iCloud.

Here what i had tried:

- Added my current local database using addPersistentStoreWithType

    NSError* error;

    NSURL* oldStoreURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
    oldStoreURL =[oldStoreURL URLByAppendingPathComponent:@"Database.sqlite"];

    // add local store (local version)

   NSPersistentStore  *store =  [self.managedObjectContext.persistentStoreCoordinator  addPersistentStoreWithType:NSSQLiteStoreType
                                                                        configuration:nil
                                                                                  URL:oldStoreURL
                                                                              options:options
                                                                                error:&error];

- Use the method migratePersistentStore to migrate (probably i'm missing something here):

    NSURL* storeURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];
    storeURL =  [storeURL URLByAppendingPathComponent:@"Database.sqlite"];

[self.managedObjectContext.persistentStoreCoordinator migratePersistentStore:store toURL:storeURL options:iCloudOptions withType:NSSQLiteStoreType error:&error];

I'm getting a Cocoa error 134080, Can't add the same store twice.

Can someone explain how this migratePersistentStore method works and what i need to pass for each parameter? I found little about it on Apple docs.

Upvotes: 0

Views: 482

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70946

The point of migratePersistentStore is that you're moving to a different store. You can't migrate to and from the same place.

Since you're migrating to iCloud, use a different name and/or path for the iCloud-enabled store. For example, name the iCloud one Database-iCloud.sqlite. Then migrate from your existing store to that one.

Upvotes: 2

Related Questions