So what happened is, I have an app which is used by Swedish(people of Sweden) and by the finns(people of Finland).

Initially when I designed the data model architecture, I just kept one entity with the name “InternationalUser” which was used to contain data of all the user.

But now due to project requirement, I need to have two different entities. One for Sweden and one for Finland and have to replace one city from both( Sweden and Finland) with another city.

So lets see how its done.

Step 1

Lets Start with creating new project. Make sure “Use Core Data” is checked

Step 2

Lets design schema of the first version of data model, which will later replaced by new version.

Step 3

In third step

I inserted some dummy data in entity “InternationalUser”.

Created table view and populated with dummy data.

So our apps looks something like this.

The text in grey is users name and text in black is their city.

Need not to tell about image.

Step 4

To migrate from one version to new version, WE NEED A NEW VERSION.

So lets create one.

To create new model version, You first open the existing data model window, then go to “Editor” and the click on “Add Model Version”

So you will see a window something like this.

First field is for the name of new model version.

And second field is tell, new version is preceded by which data model version.

Step 5

Now lets design new schema. As I said earlier, now I want two different entities for Sweden and Finland users.

So I will create, two entities with name FinlandUser and SwedenUser.

These two entities have almost same attribute as the earlier “InternationalUser” entity except one “userCountry”. We removed “userCountry” in our new data model version .

So our FinlandUser and SwedenUser entities will look something like this.

Step 6

This is probably the only step, which requires some coding. finallyyy.

So lets create a swift class with name SwedenUserMapping.

This class will be inheriting NSEntityMigrationPolicy.

NSEntityMigrationPolicy is the class, which allows us to take full control over migration process. We can do n number of things using this class, like writing custom migration logic, mapping 1 attribute with N number of attribute and vice versa, running complex logic on data etc.

PS: I could have used single class to perform mapping and migration of both the entities, but for better clarity, I created two separated NSEntityMigrationPolicy class, one for Sweden User and one for Finland user.

The only method we need to override is the createDestinationInstances.

So our SwedenUserMapping class looks something like this

As per the apple document, createDestinationInstances(forSource:in:manager:) creates the destination instance(s) for a given source instance.

here source refers to the source entity(old data model), mapping refers to entity mapping and manager is the instance of NSMigrationManager.

NSMigrationManager performs the data migration using given mapping.

So What I am doing in my custom logic is,

Checking if sIntance.Entity is InternationalUser or not. If yes move ahead.

Storing attribute value into local variables

If user country is Sweden, then create an object of NSEntityDescription Class for entity SwedenUser into destination context(new data model).

Set all the values.

As I mentioned earlier, I need to replace one city from both (Sweden and Finland) to new city. So in our SwedenUserMapping class, I am replacing city Trosa with Nora.

I wrote smilier logic in our FinlandUserMapping class.

Remember the createDestinationInstances method will only be call once to perform data migration. If data migration is completed successfully, It will never be called again.

Now we are done with the coding as well.

Step 7

The core and most important element of data migration process is the Mapping Model.

We need to have a mapping model, Which will do mapping of attributes of older version to attributes of new version.