CoreData: how to properly use migratePersistentStore to back up

I am really struggling with this. I am trying to back up my active database. According to Apple, the best option is not to use the File Manager, but the migratePersistentStore method. However, I do not understand this. I have my PersistentStoreCoordinator in an AppDelegate app. So, if I move persistent storage, will my coordinator lose it after moving it correctly? So, now the store is now in a new place, but not in the old one? So, do you have any sample program code for this, how can my application still continue to work with the original copy?

Or can I just copy all the files using the filemanager with the same prefix, and not migrate ?! Much easier...

+4
source share
1 answer

You can create a separate NSPsistentStoreCoordinator just for porting and continue to use your regular CoreData stack. You can also use NSMigrationManager for migration:

NSMigrationManager* manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel
                                                             destinationModel:targetModel];

BOOL migratedSuccessfully = [manager migrateStoreFromURL:sourceStoreURL
                                                    type:type
                                                 options:nil
                                        withMappingModel:mappingModel
                                        toDestinationURL:destinationStoreURL
                                         destinationType:type
                                      destinationOptions:nil
                                                   error:error];

Also, I'm not sure if you can migrate when your database is open, maybe you need to block it or something like that.

+1
source

All Articles