Database Reference
In-Depth Information
let
modelURL =
NSBundle
.
mainBundle
().
URLForResource
(name,
withExtension:
"mom"
,
subdirectory:
"UnCloudNotesDataModel.momd"
)
return
NSManagedObjectModel
(contentsOfURL: modelURL!)
}
}
The first method will return all model versions for a given name. The second
method will return a specific instance of
NSManagedObjectModel
. Usually, Core Data
will give you the most recent data model version, but this method will let you dig
inside for a specific version.
Note
: When Xcode compiles your app into its app bundle, it will also compile
your data models. The app bundle will have at its root a .momd folder that
contains .mom files. MOM or Managed Object Model files are the compiled
versions of .xcdatamodel files. You'll have a .mom for each data model
version.
To use this method, add the following method inside the class extension:
class
func
version1() ->
NSManagedObjectModel
{
return
uncloudNotesModelNamed
(
"UnCloudNotesDataModel"
)
}
This method will return the first version of the data model. That takes care of
getting
the model, but what about checking the version of a model? Add the
following method to the class extension:
func
isVersion1() ->
Bool
{
return
self
==
self
.
dynamicType
.
version1
()
}
You'll be able to call this method to check whether it is from version 1 when you
have a
NSManagedObjectModel
instance.
Next, add similar methods for versions 2 to 4 to the class extension:
class
func
version2() ->
NSManagedObjectModel
{
return
uncloudNotesModelNamed
(
"UnCloudNotesDataModel v2"
)
}
func
isVersion2() ->
Bool
{
return
self
==
self
.
dynamicType
.
version2
()
}
class
func
version3() ->
NSManagedObjectModel
{
return
uncloudNotesModelNamed
(
"UnCloudNotesDataModel v3"
)
}