Core Data Migrations: Evolving Your App's Schema Safely

Core Data migrations are your app's plan for evolving its local data model without losing user data. Use them when shipping a new version that adds or renames attributes. The footgun is assuming 'lightweight' migration works for everything; it doesn't.
Why it exists
Apps evolve, and so do their data models. When you release a new version of your app with a different Core Data model, the existing user data on devices is stored in the old format. Without migration, the app would crash or fail to load the data, effectively losing it. Migrations bridge this gap, ensuring data from previous versions is compatible with the new one.
The mental model
Think of a Core Data migration as a translation script between two versions of your database schema. Core Data sees the old data file (schema v1) and knows it needs to match the app's new code (schema v2). The migration provides the step-by-step instructions to transform the v1 data into a v2-compatible format, preserving all the user's information.
How it works
When your app starts, Core Data compares the schema of the persistent store file on disk with the schema compiled into your application. If they differ, it initiates a migration. 'Lightweight' migration is automatic; Core Data infers simple changes like adding a new attribute or renaming one (if you set its renaming identifier). For complex changes, you must create a 'heavyweight' migration using a Mapping Model (.xcmappingmodel) file. This file lets you define explicit transformation rules.
When to use it
Use lightweight migration for simple changes: adding or removing an attribute, entity, or relationship; making an optional attribute non-optional (with a default value); or renaming an element. Use heavyweight (custom) migration for anything more complex, such as splitting an entity, merging entities, or transforming data values during the migration (e.g., converting a string to a number).
When not to use it
You don't need migrations for an app's first release or if the data model will never change. The main footgun is attempting a change with lightweight migration that requires a heavyweight one. For example, changing an attribute's type from String to a custom Transformable type requires a custom mapping model. The migration will simply fail at runtime, potentially leading to data loss if not handled gracefully.
One canonical example
Your v1 app has a Note entity with a creationDate as a String. In v2, you change it to a Date type for better sorting. A lightweight migration will fail because Core Data doesn't know how to parse the string into a date. You must create a custom migration with a mapping model that specifies how to transform the source string into the destination Date object, likely using a custom entity migration policy class.
Interview question
Which data model change in Core Data would necessitate a heavyweight migration rather than a lightweight one?
- a.Adding a new optional attribute to an existing entity.
- b.Changing an attribute's type from String to Date.Correct
- c.Renaming an existing entity using a renaming identifier.
- d.Making an optional attribute non-optional and providing a default value.
Why? this is the answer
The card explicitly states that changing an attribute's type, such as from String to Date, requires a custom (heavyweight) migration because Core Data cannot automatically infer the transformation. The other options are all scenarios handled by lightweight migration.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles