Swift — 4 — Core Data — Part 5 Core Data Migration

Hello readers. Hope you are doing good.  😀

Today we will discuss about core data migration. In the development phase of a project we keep on adding new features in every versions of app. These feature includes new business requirements and to meet these requirement we need to update our data models/entity.

Let’s say we have already uploaded app on apple store. This application uses core data to save person name and social security number (SSN).  Now in the next version of the app we want to add new attribute for person model/entity.  Let’s say we added new attribute in the model, write the relevant code, test and upload the application on app store. Now when users will update their app with newer version they may loss existing data  😡  And this is what we do not want to happen. How to solve this problem ? Answer is Core Data Migration.

Please support me by comment, like and share my facebook page.

Core Data Migration

In order to follow core data migration we should keep on versioning our .xcdatamodeld file. Also to address new changes we should not make any changes in existing data model. Instead we should create a new version of .xcdatamodeld and perform changes there. We should follow core data migration if there are changes in:

Entity

  • Add a entity
  • Remove a entity
  • Renaming a entity

Attribute

  • Add an attribute
  • Remove an attribute
  • Renaming an attribute

Relationship

  • Add a relationship
  • Remove an relationship
  • Renaming an relationship

Let’s start the migration process. Down the starter project here.

Let’s say due to the new requirement, we should have one more attribute named “hobby” in Entity. Instead of making any changes in existing Person model we will follow a database migration process.

Open project and select PersonData.xcdatamodeld. At this point it should look like this:

Now click on Editor -> Add model version like this:

After click on Add model version, we will be asked for the base xcdatamodeld:

In above screenshot we can give a version name and choose the base model. Click Next:

Look at the files generated on left hand side. It says PersonData.xcdatamodeld have two versions – base version (with green tick mark) and new version.

Now lets set the Model Version to be the new one. Choose PersonData 2 from list. See attached screen shot:

After choosing  PersonData 2, the current version for xcdatamodeld becomes PersonData 2 like below attached image:

Notice the new xcdatamodel is set up with new version (PersonData 2.xcdatamodel).

Now let’s add some code in core data manager class and  a new attribute named hobby in new version of model.

Notice the change in CoreDataManager.swift file’s.

Explaination

NSPersistentStoreDescription object is used to create and load a persistent store.

shouldMigrateStoreAutomatically is a flag indicating whether the associated persistent store should be migrated automatically.

shouldInferMappingModelAutomatically is a flag indicating whether a mapping model should be created automatically.

That’s it! We are done with core data migration. It is also called light weight migration. I will also write about the manual migration when i will get time. But most of the time light weight migration will just do the work.

Happy coding!

Here is the source code.

Please support me by comment, like and share my facebook page.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.