The Migration Process
During migration, Core Data creates two stacks, one for the source store and one for the destination store. Core Data then fetches objects from the source stack and inserts the appropriate corresponding objects into the destination stack. Note that Core Data must re-create objects in the new stack.
Overview
Recall that stores are bound to their models. Migration is required when the model doesn't match the store. There are two areas where you get default functionality and hooks for customizing the default behavior:
When detecting version skew and initializing the migration process.
When performing the migration process.
To perform the migration process requires two Core Data stacks—which are automatically created for you—one for the source store, one for the destination store. The migration process is performed in 3 stages, copying objects from one stack to another.
Requirements for the Migration Process
Migration of a persistent store is performed by an instance of NSMigrationManager
. To migrate a store, the migration manager requires several things:
The managed object model for the destination store.
This is the persistent store coordinator’s model.
A managed object model that it can use to open the existing store.
Typically, a mapping model that defines a transformation from the source (the store’s) model to the destination model.
You don’t need a mapping model if you’re able to use lightweight migration—see Lightweight Migration.
You can specify custom entity migration policy classes to customize the migration of individual entities. You specify custom migration policy classes in the mapping model (note the “Custom Entity Policy Name” text field in Figure 4-1).
Custom Entity Migration Policies
If your new model simply adds properties or entities to your existing model, there may be no need to write any custom code. If the transformation is more complex, however, you might need to create a subclass of NSEntityMigrationPolicy
to perform the transformation; for example:
If you have a Person entity that also includes address information that you want to split into a separate Address entity, but you want to ensure uniqueness of each Address.
If you have an attribute that encodes data in a string format that you want to change to a binary representation.
The methods you override in a custom migration policy correspond to the different phases of the migration process—these are called out in the description of the process given in Three-Stage Migration.
Three-Stage Migration
The migration process itself is in three stages. It uses a copy of the source and destination models in which the validation rules are disabled and the class of all entities is changed to NSManagedObject
.
To perform the migration, Core Data sets up two stacks, one for the source store and one for the destination store. Core Data then processes each entity mapping in the mapping model in turn. It fetches objects of the current entity into the source stack, creates the corresponding objects in the destination stack, then recreates relationships between destination objects in a second stage, before finally applying validation constraints in the final stage.
Before a cycle starts, the entity migration policy responsible for the current entity is sent a beginEntityMapping:manager:error:
message. You can override this method to perform any initialization the policy requires. The process then proceeds as follows:
Create destination instances based on source instances.
At the beginning of this phase, the entity migration policy is sent a
createDestinationInstancesForSourceInstance:entityMapping:manager:error:
message; at the end it is sent aendInstanceCreationForEntityMapping:manager:error:
message.In this stage, only attributes (not relationships) are set in the destination objects.
Instances of the source entity are fetched. For each instance, appropriate instances of the destination entity are created (typically there is only one) and their attributes populated (for trivial cases,
name = $source.name
). A record is kept of the instances per entity mapping since this may be useful in the second stage.Recreate relationships.
At the beginning of this phase, the entity migration policy is sent a
createRelationshipsForDestinationInstance:entityMapping:manager:error:
message; at the end it is sent aendRelationshipCreationForEntityMapping:manager:error:
message.For each entity mapping (in order), for each destination instance created in the first step any relationships are recreated.
Validate and save.
In this phase, the entity migration policy is sent a
performCustomValidationForEntityMapping:manager:error:
message.Validation rules in the destination model are applied to ensure data integrity and consistency, and then the store is saved.
At the end of the cycle, the entity migration policy is sent an endEntityMapping:manager:error:
message. You can override this method to perform any clean-up the policy needs to do.
Note that Core Data cannot simply fetch objects into the source stack and insert them into the destination stack, the objects must be re-created in the new stack. Core Data maintains “association tables” which tell it which object in the destination store is the migrated version of which object in the source store, and vice-versa. Moreover, because it doesn't have a means to flush the contexts it is working with, you may accumulate many objects in the migration manager as the migration progresses. If this presents a significant memory overhead and hence gives rise to performance problems, you can customize the process as described in Multiple Passes—Dealing With Large Datasets.
Copyright © 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-01-09