Database Reference
In-Depth Information
versioning works with those same MOM objects but takes the design one step
further. Each entity version in each data model has a unique hash. When
Core Data loads a persistent store from disk, it resolves the matching hashes
in the persistent store against the MOM objects included with the application.
If the matching MOM is not flagged as the “current” MOM, data migration
kicks in.
How Data Migration Works
Core Data handles data migration in a very straightforward manner. When
a persistent store needs to be migrated, Core Data performs three steps.
Copying the Entities with Attributes
In the first pass of the migration, Core Data creates new entities in the new
persistent store for every entity in the old store. These entities have their
attributes copied over, but not their relationships. During this phase, Core
Data also keeps a reference to the old unique ID for each entity to be used in
phase 2.
Creating Relationships Between the Entities
In the second pass, Core Data builds all the relationships between the entities
based on the previous relationships. This is where the reference in phase 1
is used.
Validating the New Store
During the migration, all validation rules are turned off, and Core Data ignores
the child classes defined in the MOM. Therefore, it is possible that some data
validation rules may have been broken during the migration. In the final phase
of the migration, Core Data goes back through the store and checks all the vali-
dation rules in order to ensure the data is in a valid state.
Model Versions and Hashes
The word versioning has been used through this chapter as well as other
material to describe data migration in Core Data. Unfortunately, it is an
inaccurate term. Versioning implies that there is an order or precedence to
the models. This is not accurate when it comes to data model versioning/migra-
tion in Core Data.
Entity Hashes
Instead of keeping track of a version number, creation date, or some other
potentially chronological identifier, Core Data generates a hash for each
entity in a model. The hashes are then stored within the persistent stores
 
 
Search WWH ::




Custom Search