Reputation: 1745
I've got big conception problem when it comes for updating my application. Here is what I've got now:
Models between version 1 and 2 of the application differs in the manner that some relationships are deleted and some models aren't used (and deleted as well). I've just moved user settings to User defaults instead of fetching them from DB which was a costly operation.
Now I've want to realease an update of my app (1) with my second version (2). I don't know how to make a migration in this scenario thou. I want to preserve the user settings and move them from model (1) to user defaults and repleace model (1) with model (2).
I've read about migration in core data, but the examples all reflect on working with single project and creating the another version of core data models, which I'm unable to do as the (2) is completly new project (ofcourse I have access to source code of(1)).
I was thinking about (correct me if there is better solution) of using two databases, fetching user settings from (1) and writting them to user defaults, then deleting database (1) and use only (2). If this is the correct way in my case, how I can use two different xcdatamodeld files in single project?
Upvotes: 0
Views: 107
Reputation: 11444
CoreData lightweight migration allows you to easily update your database when there are minor changes to the schema. When you're making larger changes then you need to use the migration mapping process.
Apple documents the process in https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSMigrationManager_class/Reference/NSMigrationManager.html#//apple_ref/occ/cl/NSMigrationManager
NSMigrationManager
is the key. You can create a subclass that handles the custom migration as you see fit. The main method to focus on is:
migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:
which allows you to specify the source database (your version 1) and the destination database (version 2).
This references a NSMappingModel
which allows you to define how the data in the two databases relate to each other. Xcode has a handy graphical interface for this process. Just create a new Mapping Model from the new file dialog.
Once the migration is done you can safely delete your old CoreData store.
Then when you create your new persistent store coordinator you can check to see if the original store is present, if so perform your migration.
Upvotes: 1