Versioning Core Data data models in XCode 5

CoreData in our apps

When we begin working in an iOS app in which we are going to need to persist a big amount of data, one of the recommended options is CoreData.

CoreData is a persistence framework totally different to the other available options to persist data on iOS, it will allow us to use the data as objects, forgetting all the related with how to store the data or how to obtain them. We just are going to store objects with concrete data, and the we will recover them in a very simple way.

The objects stored in Core Data are defined in XCode as entities, with some attributes and/or relationships between them, as an example, we can have an entity called Library, which has a relationship To-Many with the entity Book, it will be enough to access the porperty books of Library to get those books, saving us to make the typical queries to databases.

Why do we need our data model to be versionable?

Because all of that sounds great, but we have also to think that is going to be probably (in fact, it will be the common situation) that in the future we would need to modify any entity, adding new attributes or relationships with other entities, or creating new entities.

If our data model is not prepared for that situation, when we will update our app with the new data model, if the user downloads the update and loads the new one, the app will crash and the user will lose every data stored on it (and we will probably lose a user).

In order to avoid tragedy, XCode allows us to make versions of our Core Data models, so that when we update it, it will migrate the previous version to the new one automatically.

There are different kinds of migrations, and different ways of customizing the process. In our case we are going to use the most simple an automatic one, the so-called Lightweight Migration.

How can we do that?

First of all, we have to take a look at the Core Data methods created automatically by XCode when we create a project in which we select the option of using it. We are specifically look at the method  (NSPersistentStoreCoordinator *)persistentStoreCoordinator

This method returns the persistent store coordinator, which is going to manage the operations with the data, and in case it is not created, it will do it. When creating it, it will add the database as we know it, an SQLite one, and while creating it we can use some options to the creation. By default it doesn’t use any options.


if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { 
        // In case of error here would be our way of controling it
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort(); 
} ;

Well, this call to the method addPersistentStoreWithType is the one that we are going to modify adding the necessary options for our data model to be versionable.

We are going to add these options to a dictionary in the following way, for using it as a parameter in the method named in the last paragraph.


NSDictionary *options = @{ 
        NSMigratePersistentStoresAutomaticallyOption : @YES,
        NSInferMappingModelAutomaticallyOption : @YES
};
 if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
 ...

The first of the options tells Core Data that it has to try to migrate the previous data model to the new one, and the second one tells it that it has to create the model automatically.

Coredata1

At this point our data model supports version migration. So let’s put all that in practice. Following with out first examen, we had an entity Library and another entity Book and they are related with the books relationship:

Here we can ser that Library has a To-Many relationship with Books and Books has a To-One relationship with Library.

Ok, after having our first version of the data model done, and counting with the fact that the users have data in their apps, let’s suppose that we want to extend the information adding data about the authors. How can we do that for having the datamodel well migrated? We have to follow some steps:

First of all we have to select our Core Data file, it’s the one which extension is .xcdatamodelId and after that we have to go to the XCode menu Editor and select the option Add Model Versión.

Once we have this done we insert a name for the new version of the data model and when we select OK we can see that the file .xcdatamodelId now has subfiles of the type .xcdatamodel, those are our versions of the data model.

The next thing to do is select the new version, and on the right menu, we choose the menu Model Version and click on the name of our new version.

Coredata1

Finally, we just have to modify the data model and run the new version, the data would be migrated automatically from one data model to the other without losing any information.

Coredata1

More info:

Leave a Comment

¿Necesitas una estimación?

Calcula ahora

Privacy Preference Center

Own cookies

__unam, gdpr 1P_JAR, DV, NID, _icl_current_language

Analytics Cookies

This cookies help us to understand how users interact with our site

_ga, _gat_UA-42883984-1, _gid, _hjIncludedInSample,

Subscription Cookies

These cookies are used to execute functions of the Web, such as not displaying the advertising banner and / or remembering the user's settings within the session.

tl_3832_3832_2 tl_5886_5886_12 tve_leads_unique

Other