How should I migrate my core data? - iphone

My core data store in the previous version to my app is different to the current one in the following ways:
Attributes missing
New attributes
Attributes which need changing
New entities
New relationships
Attributes which need moving between entities
How should I go about migrating my data? What I had in mind before looking into this was that I could just write code that could take the data from the previous model and put it in place in the new model.

I think the Core Data Model Versioning and Data Migration Programming Guide will cover these issues in some detail.

Related

Core Data Migration: Do I Need a new Mapping Model for each new Model Version I Add?

I've done a custom Core Data migration several versions back when doing some structure changes in my app. (So created a new model version, and a mapping model with custom policy class).
Now, I want to do some more changes. So I've created another model version. Now, I'm not sure whether I need to create another mapping model for this change? If I do, will core data just figure out the appropriate one to use based on the users version?
Will I also need to create another custom policy class, or can I somehow add the new logic to the first one?
Lastly, will I need to add any logic for migrating from the original database straight to the current database? Or will core data figure that out for me, and migrate to the median version first, and then to the current version when a user loads an app version with the original data structure?
Thanks!
I guess the answer to whether or not you need to create another mapping model is... it depends. See Apple's docs here (specifically comments on lightweight migration): https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/CoreDataVersioning/Articles/vmMigrationProcess.html#//apple_ref/doc/uid/TP40004399-CH6-SW1
Depending on your model changes, you may use "The Default Migration Process". When calling addPersistentStoreWithType:configuration:URL:options:error:, use the flag NSMigratePersistentStoresAutomaticallyOption.
Also, I'm sure that unfortunately you need one mapping model for each possible migration. If you have 3 models, you'll have to implement: 1 to 3 and 2 to 3. Core Data is not smart enough to do the intermediate steps automatically :(

Is it possible to generate a Core Data structure programmatically?

I am currently working on a project which may require the ability to generate a Core Data model and classes automatically from a JSON feed which describes each object, it's properties and relationships. This would enable our company to quickly generate a working core data database from our CRM at the start of each project.
Is this possible/easy to achieve? If so, does anyone have any tips on where I can get started?
I have already created various iOS apps which use Core Data but I have never made one which needs to automatically create a Core Data model.
Yes, you can create a Core Data model dynamically. An NSManagedObjectModel can be created without a stored model, and NSEntityDescription/NSAttributeDescription instances can be defined and added to it before being passed to an NSPersistentStoreCoordinator. Apple has an example of doing this in their Core Data Utility Tutorial.

NSInferredMappingModelError when migrating Core Data store with entity that has relationshio changing from to-many to to-one

I get an NSInferredMappingModelError when I try to migrate my model.
During the migration, the objects of an entity Example are migrated from having a to-many relationship to a to-one.
The migration fails with the error when it tries to make an NSMappingModel via inferredMappingModelForSourceModel.
I need to use custom migration because of the nature of the project - the program operates on (ideally) any kind of model.
Any ideas or nudges in the right direction?
Because lightweight migration cannot deal with the change you've made, inferredMappingModelForSourceModel is going to fail. You have to create the mapping yourself and load it with mappingModelFromBundles.
It is hard to go into a lot of details here without knowing more specifics but you can learn more about custom migration in Apple's Core Data Model Versioning and Data Migration Programming Guide. You might want to start by looking at the sections "Customizing the Migration Process" and "Mapping Overview".

Core data versioning and migration

I have the older version of the core data model of the released app. Now, in the next version of the app, I am migrating the core data model to new model. Is there a way to get the attribute values from the old model before actually migrating to the new version of the model or is there possibly a way to know if the migration is to be occurred.
When you migrate, Core Data actually moves existing values for you. If you're doing a relatively simple migration, such as adding or removing some properties, Core Data does its best to make the move as seamless as possible. In many cases, you won't need to make any changes other than set your new model. See my answer here for more.
Yes, it is possible.
To get the attribute values from the old model you'll need to create custom entity migration policies (NSEntityMigrationPolicy subclasses). Then in -createDestinationInstancesForSourceInstance:entityMapping:manager:error: you'll receive source instance, and it is completely up to you how to create destination instances from source instances.
There is a method to check if a given store is compatible with store coordinator model: -[NSManagedObjectModel isConfiguration:compatibleWithStoreMetadata:].

iPhone:Updating a Core Data Model in Future Versions of an App

I am releasing a Core Data based app and wondered what I need to consider if, in an update later down the line, I need to change the model.
How do I move existing user data on the device from the old data model into a new updated model?
Thanks
Generally, you'll have to consider nothing now but as soon as you change your data model, you'll have to employ Core Data's model migration tools. Read the Core Data Model Versioning and Data Migration Programming Guide.