Database Reference
In-Depth Information
init
(modelName:
String
, storeName:
String
,
options:
NSDictionary
? =
nil
) {
self
.
modelName
= modelName
self
.
storeName
= storeName
self
.
options
= options
}
In setting the default value of
options
to
nil
, the old method signature remains
valid and you have the additional choice of passing in the options dictionary.
Find the
coordinator
computed property and change the initialization of the
persistent store as follows:
store
= coordinator.
addPersistentStoreWithType
(
NSSQLiteStoreType
,
configuration:
nil
,
URL:
storeURL
,
options:
self
.
options
,
error:
nil
)
There's just a small change here to pass in the extra options when creating the
stack.
Open
NotesListViewController.swift
and change the
CoreDataStack
lazy
initialization statement to use the lightweight migration options:
lazy
var
stack :
CoreDataStack
= CoreDataStack(
modelName:
"UnCloudNotesDataModel"
,
storeName:
"UnCloudNotes"
,
options:[NSMigratePersistentStoresAutomaticallyOption:
true
,
NSInferMappingModelAutomaticallyOption:
true
])
The
NSMigratePersistentStoresAutomaticallyOption
is what tells Core Data (the
NSPersistentStoreCoordinator
, actually) to start a migration if the persistent store
model isn't compatible with the current data model. Core Data will handle all the
details, from finding the source model to creating the destination store file, and all
the steps in between.
The
NSInferMappingModelAutomaticallyOption
is the other half of what makes a
lightweight migration possible. Every migration requires a mapping model. Here's
an analogy: If you're traveling from a known place on Earth to somewhere
unknown, you'll want a map to tell you where to go. The mapping model is that
guide.
It just so happens that Core Data can infer a mapping model in many cases. That
is, Core Data can automatically look at the differences in two data models and
create a mapping model between them. For entities and attributes that are identical
between model versions, this is a straightforward data pass through mapping. For