Database Reference
In-Depth Information
init
(storeNamed:
String
, modelNamed:
String
) {
self
.
storeName
= storeNamed
self
.
modelName
= modelNamed
}
}
You'll keep track of the store name and model name with two properties. There is
also an optional options dictionary property for use when performing the final
adding of the store. As seen from previous migrations, this options dictionary will
become more important when you finally get to the step where you add a
persistent store to a coordinator.
Next, open
NotesListViewController.swift
and replace the stack lazy initialization
code with the following:
lazy var stack : CoreDataStack = {
let manager = DataMigrationManager(
storeNamed:"UnCloudNotes",
modelNamed:"UnCloudNotesDataModel")
return manager.stack
}()
First of all, you only want the stack to be “initialized” once, so the lazy attribute
takes care of that. Second, the initialization is actually handled by the
DataMigrationManager
, so the stack used will be the one returned from the
migration manager.
Now to the harder part: How do you figure out if the store needs migrations? And if
it does, how do you figure out where to start? To do this, you're going to need
helper methods. At the bottom of
DataMigrationManager.swift
, add an
extension on
NSManagedObjectModel
:
extension
NSManagedObjectModel {
class
func
modelVersionsForName(name:
String
)
-> [
NSManagedObjectModel
] {
let
urls =
NSBundle
.
mainBundle
().
URLsForResourcesWithExtension
(
"mom"
, subdirectory:
"
\(name)
.momd"
)
as
[
NSURL]
return
urls.
map
{ url
in
NSManagedObjectModel
(contentsOfURL:url) }
}
class
func
uncloudNotesModelNamed(name:
String
)
->
NSManagedObjectModel
{