Database Reference
In-Depth Information
Figure 8—Setting the current model version
Now that we have a new version of the MOM, it's time to add the new entities
and attributes. This process requires the addition of a new entity and some
changes to the
Recipe
entity. Comparing the v1 data model and the v2 data
model (
Figure 9,
Versions 1 and 2 of the data model
, on page 43
), you can see
that we added a new
Author
entity along with its one-to-many relationship with
the
Recipe
entity. Also, the
Recipe
entity has a new attribute called
lastUsed
, which
is defined as a
Date
.
We're not quite done. If we were to run the application right now, we would
trip the error that we discussed in
Section 3.1,
Some Maintenance Before We
Migrate
, on page 38
. Clearly, something is missing.
Turning on Automatic Data Migration
The first thing we need to do is to tell Core Data to automatically migrate data
when the persistent store is not using the same model as the current version.
To do this, we need to make a small change to the
persistentStoreCoordinator
method in our
AppDelegate
. Previously we were passing
nil
to the
addPersistentStore-
WithType:configuration:URL:options:error:
method for the
options
parameter. However,
we need to change that to the following: