Database Reference
In-Depth Information
The migration process
When you initialize a Core Data stack, one of the steps involved is adding a store to
the persistent store coordinator. When you encounter this step, Core Data does a
few things prior to adding the store to the coordinator.
First, Core Data analyzes the store's model version. Next, it compares this version
to the coordinator's configured data model. If the store's model version and the
coordinator's model version don't match, then Core Data will perform a migration,
when enabled.
Note: If migrations aren't enabled, and the store is incompatible with the
model, Core Data will simply not attach the store to the coordinator and
specify an error with an appropriate reason code.
To start the migration process, Core Data needs the original data model and the
destination model. It uses these two versions to load or create a mapping model for
the migration, which it uses to convert data in the original store to data that it can
store in the new store. Once Core Data determines the mapping model, the
migration process can start in earnest.
Migrations happen in three steps:
1. First, Core Data copies over all the objects from one data store to the next.
2. Next, Core Data connects and relates all the objects according to the relationship
mapping.
3. Enforce any data validations in the destination model. Core Data disables
destination model validations during the data copy.
You might ask, “If something goes wrong, what happens to the original source data
store?” With nearly all types of Core Data migrations, nothing happens to the
original store unless the migration completes without error. Only when a migration
is successful, will Core Data remove the original data store.
Types of migrations
In my own experience using Core Data, I've found there are a few more migration
variants than the simple distinction between lightweight and heavyweight. Below,
I've provided the more subtle variants of migration names, but these names are not
official categories by any means. I'll start with the least complex form of migration
and end with the most complex form.
Lightweight migrations
A lightweight migration is Apple's term for the migration with the least amount of
work involved on your part. Simply enable a couple of flags when setting up a Core
Data stack, and the migration happens automatically. There are some limitations on
 
Search WWH ::




Custom Search