What is core data migration and how to do it?


 

In the development phase of a project we keep on adding new features in every version of the app.
These features include new business requirements and to meet these requirements we need to update our data models/entities.

Let’s say we have already uploaded an app on the apple store. This application uses core data to save a person's name and social security number (SSN). Now in the next version of the app we want to add a new attribute for person model/entity. Let’s say we added a new attribute in the model, wrote the relevant code, tested and uploaded the application on the app store. Now when users update their app with newer versions they may lose existing data :x And this is what we do not want to happen. How to solve this problem?

The answer is Core Data Migration.

Types of migration: 

  1. Lightweight migration

  2. Heavyweight migration

What can we migrate?

Attribute, Entity & Relationship

  • Add

  • Remove

  • Renaming


Steps to migrate core data.

Step 1. Visualize your current Attribute, Entity & Relationship.



Step 2. Add a new model version. go to “Editor” and then click on “Add Model Version”.




Step 3. Naming your new version.


Step 4. Change your current model version to the newly created model.









Step 5. Now add, update, delete your new version of model according to your new requirement




Step 6. It is optional. Just need to change a line of code in AppDelegate.

For Lightweight migration no need to change anything.





For Heavyweight migration `shouldMigrateStoreAutomatically = false`


That's all 7 steps you have to do for Lightweight migration.

#Some add on to learn 

  • How to find Core data sqlite path.

  • How to find sqlite file in finder.

  • How to check/insert/update/delete data in SQlite database using DB Browser UI.












Comments