Hey everybody this is Caleb with Dev slopes and in this video we're going to be talking about core data.
Obviously it's the most important part of this section but we're going to be talking about what even
is core data and how does it work so that when we use it we understand what's going on behind the scenes.
So to begin I just want to show you this graphic that I've built for us to help explain the concept
of the core data stack.
Ok so what you need to know is that the core data stack contains three main objects the managed object
model the managed object context and the persistent store coordinator.
Now if you know anything about core data already you know that its purpose is for persistence storage
we can store things with core data and then access them later without them being destroyed every time
that our app is closed or quit or if our phone restarts the core data allows us to persistently store
data.
It's really really cool now to begin.
Every core data app has a managed object model.
OK.
Now what the Met managed object model does is it pulls our data model into the core data stack.
So it basically takes our data model and pulls it into the core data stack.
Now the managed object context is what's going to do all of the work.
And as a developer you're actually going to be interacting with this most of the time.
OK.
And when you use core data in general you're going to be dealing with the managed object context.
OK now something you need to know is that the managed object contex keeps a reference to the persistent
store coordinator and I'm going to explain why that's important in a moment.
But as you would expect the persistent store coordinator coordinates the persistent storage of data
in our app K and it basically holds a persistent store that has all of our information that we save
and want to retrieve later on.
Now what this does is it understands our data model and it manages storing that information persistently.
OK.
Very very cool.
Now to create a core data stack what we first need to do is to instantiate the data model which is used
to create a managed object model in our core data stack.
OK.
Now the managed object model is required to instantiate the persistent store coordinator.
OK.
Now the persistent store coordinator first needs to understand the data model.
That's why this comes second.
The object model is created first and then because that's created the persistent store coordinator can
be created.
It needs to understand the model and make sure that our persistent storage is compatible with it.
If our if our object or if our managed object model were to change and suddenly be incompatible with
the persistent store it needs to know that that won't work and then throw the proper error.
OK.
Now what happens is the persistent store coordinator creates a persistent store once it knows that it's
compatible.
And that's why it needs to reference the managed object model because it's kind of a way to verify and
I'm sure you're noticing that there are kind of steps and prerequisites in order to get up to the top
of the core data stack where we interact with it.
And this is for safety and also just to make sure that everything works as smoothly as possible.
Now our application works with the managed object context.
OK.
And it's basically going to access the managed object context and the main subject context is sort of
like the brain of core data because it manages our collection of managed objects.
OK.
If the managed object context needs to load data from persistent storage where all of our data is saved
basically it asks the persistent store coordinator for it and then the well I'll draw a little arrow
here the managed object context ask the persistent store coordinator if the persistent store is compatible
with our data model it will return the data that we are asking for which is really neat if it needs
to save data like let's say that our app changes some data.
The managed object context will pass that into the persistent storage coordinator and then that tells
the persistent store to change and to be modified then it stores it persistently and we can access it
or save to it as much as we want.
It's really really cool.
So as you can see all of this is just a big ladder sort of and there are prerequisites in order to instantiate
each of these elements so we create a data model which is pulled into the managed object model k then
that is used to instantiate the persistent store coordinator which then checks to make sure that the
persistent store is compatible with our data model.
If it is then the managed object context can pull that data and displayed in our app if we want to save
data we can push that back down through the managed object context and then into the persistent store.
This is how core data works as kind of a Barela kind of an overview of how it works and of course as
always with any of these topics you can always dive in deeper research deeper and find out so much more
about how this amazing technology works.
Apple has done an incredible job giving us core data.
And so I'm really excited to teach you how this works and how we can basically use all of these to build
an amazing
So let's go ahead and let's move on to the next video where we're actually going to create our first
Chordata data entity and create some core data attributes as well.
We're going to talk about the core data model and how we can use it as a developer in X code.
So let's head on over to that video and let's build that now.