DevPrep

All topics › Databases & storage

How do you migrate Core Data?

Senior PRO

Essence

A migration is needed when the NSManagedObjectModel changes while devices already have stores of the old version. Core Data stores the model's hash in the file and compares it with the current model on open; if they differ it either migrates or fails with NSPersistentStoreIncompatibleVersionHashError. The rule: never edit the current model version in production — add a new version (Editor → Add Model Version) and make it current.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store