简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM