Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Part of initializing our Core Data stack involves taking the object graph and creating a schema or outline of it for Core Data to work with. In this video, we look at how we can easily achieve this using the NSManagedObjectModel class.
-
0:00
The second part of our Core Data stack is the managed object model.
-
0:05
It's important to remember that the managed object model
-
0:07
is not the model objects themselves but the entire object graph.
-
0:12
Think of it this way, each model, lets a for example,
-
0:15
an employee class is a managed object.
-
0:18
Our object graph could contain three manage objects, employee, manager, and
-
0:23
department.
-
0:24
The managed object model, which we're about to create, is a schema or
-
0:30
outline of all the managed objects and their relationships to one another.
-
0:34
Remember I said that Core Data is good at maintaining these relationships.
-
0:38
Well, that's what the managed object model does.
-
0:41
While the managed object model is created in code,
-
0:44
we'll actually use a graphical user interface,
-
0:46
a GUI editor to create each managed object, but more on that in a bit.
-
0:51
The managed object model is represented by the class NS managed object model.
-
0:56
And to use this class in our code, we need to import the Core Data framework.
-
1:03
So up at the top here in this file we'll say, import CoreData.
-
1:08
We'll create the managed object model as a lazy store property with a closure
-
1:12
just like we've been doing.
-
1:14
So we'll say, lazy var managedObjectModel
-
1:20
of type NSManagedObjectModel.
-
1:28
Just as in FYI, if you're doing this pattern where you are specifying the type
-
1:32
and then assigning a closure that you call immediately, if you don't assign this
-
1:37
type it's not gonna work first of all, because it can't infer it.
-
1:41
And secondly, if you don't go ahead and return the object of that type
-
1:45
immediately, you might run into these cases where the compiler doesn't know how
-
1:49
to auto complete or it returns an error on some of these things.
-
1:53
And that's again because it can't infer the type.
-
1:56
So just keep in mind that you're not doing anything wrong,
-
1:58
it's just the compiler trying to figure things out.
-
2:01
The body of the closure is pretty simple.
-
2:03
Since we'll be creating the managedObjectModel using a GUI editor,
-
2:07
we can initialize the managedObjectModel in code from a file.
-
2:11
But first we need a URL for the file that we're going to initialize it with.
-
2:15
We haven't created this file yet, but let's go ahead and obtain a URL for it.
-
2:20
We can do that because we know the name, since when we created that file,
-
2:23
we'll give it the same name.
-
2:24
We're going to use the bundle class to find the URL for
-
2:27
a resource in the main bundle.
-
2:29
So let modelUrl = Bundle.main.url for resource,
-
2:35
and this is going to take a resource name and an extension.
-
2:41
So here we'll say TodoList, uppercase L.
-
2:45
And the extension is momd, so M-O-M-D.
-
2:50
And we're going to force unwrap this, because it creates an optional.
-
2:55
Now the resource name here is TodoList and
-
2:57
the extension momd we'll talk about that more in a second.
-
3:01
Now that we have a URL that we've unwrapped, let's create and
-
3:04
return a managed object model.
-
3:06
So we'll simply say return NSmanageObjectModel.
-
3:10
It has an initializer that takes contents of a URL and we'll pass in the model URL.
-
3:18
Now again this initializer is a failable one,
-
3:21
if we can option click it we don't, we get an error.
-
3:25
So what we're gonna do is force unwrap this as well.
-
3:28
Now, this is unsafe if you're thinking that you're right.
-
3:31
But in this case,
-
3:32
we actually do want the program to crush if we can't load our managed object model.
-
3:36
Because then we can't use any of these.
-
3:39
And that is the managed object models done.
-
3:42
Let's make one quick change here.
-
3:44
When we use this Core Data stack in the rest of our app,
-
3:47
we don't want anyone modifying these properties that application document
-
3:51
directly in the managed object model and so on.
-
3:54
So we are going to make them private properties.
-
3:58
Now we've learned how to make properties and methods private before.
-
4:01
And we used a private keyword to do that.
-
4:03
But again there's more nuance to it and we've touched on this before.
-
4:06
But if I want these properties to be readable publicly but not setable.
-
4:11
So essentially what we want to do is make the setter private, but the getter public.
-
4:16
To do that, we'll say private[set].
-
4:18
And we've touched on this before, but it's a nice reminder.
-
4:21
So we'll copy that and paste it down here as well.
-
4:23
So now we could read these properties if we wanted to,
-
4:26
from this core data stack class, but we can't set it.
-
4:29
So this ensures that we can only modify the properties from inside the class.
-
4:34
The NSManagedObjectModel instance
-
4:36
describes the data that is going to be accessed by the Core Data stack.
-
4:41
During the creation of the stack,
-
4:43
the NSManagedObjectModel often referred to as the MOM, for managed object model,
-
4:49
is loaded into memory as the first step in the creation of the stack.
-
4:54
Now once this object is initialized, the coordinator object is constructed.
-
4:58
So let's go to that next.
You need to sign up for Treehouse in order to download course files.
Sign up