Hey everybody this is Caleb began with slopes dot com.
And in this video I'm so excited we're going to create our core data model.
This is going to allow us to create a core data entity and give it some core data attributes as well.
And basically this is going to be our data model how we're going to store objects of type goal in our
app and then later we'll be able to fetch them and pull them all up into our table view to display them.
Really cool stuff.
Let's go ahead and let's pull open our X code project and let's go set it up.
OK.
So to do that what we have done already is when we built this project from the beginning when we created
it we ticked include core data so that automatically creates what's called an X C data model D and it
is the same name as our project so I'm actually going to close the assistant editor here and this is
it.
This is the core data interface.
Now there are a few things.
Let me just show you around a little bit.
There's a section here for entities will talk about that in a second.
There's a section for fecche requests.
We're not going to use this feature.
We'll do it in code but we're not going to do it here.
And then there are configurations.
K now this displays your entities.
And later we'll show you the attributes section.
This is of course on the side is just some information and this will I will show you this isn't just
a second as you see when I hover over it.
It'll pop up a name here and it is the data model inspector that is.
We'll use that in just a little bit.
But for now let's go ahead and let's create our entity think of this just like a model layer for any
other app that you're going to build.
OK.
An entity is like a model or like a class can then.
Attributes are just like properties.
So go ahead and click add entity and we get a blank entity.
Now there are attributes relationships and fetched properties in this app we're only going to focus
on attributes but relationships and fetch properties.
I really encourage you to dive in deep and get to know what those are.
OK.
Now our entity we don't want to name it entity because that's just kind of a generic name.
We're going to rename it.
So right click or sorry click on it once and then you can basically edit the name Kay.
So this will not be called entity but instead we're going to call it goal.
OK.
Think of this as if we were to make a model layer called goal we would set up some properties and we
would create an initialiser to set those values whenever we instantiate an instance of our goal model.
But we're going to use core data instead of doing that it's really cool.
So we think of goal as our class and the attributes are like our properties so what does a goal need.
Click an item and this is our first property.
But in core data it's called an attribute.
OK.
So we're going to call this goal description.
OK.
And it's type.
We need to set explicitly right here.
Now of course like before the goal description is just text so we can give it a value of type string.
You'll notice on the side here in the data model inspector you can see that we can give it a name we
can set some properties here the attribute type.
And there's all kinds of complex validation and a default value we can set.
There's all kinds of complex stuff that you can set in here.
But for now we just need to give a name and said it's type.
So let's keep going.
Let's go ahead and set a goal type and that is also going to be string.
OK.
Now that's for short term or long term which of course is textual data.
So we need to use a string came very cool.
Next we're going to go ahead and create a new attribute.
We're just we're still thinking of these like properties in a class.
OK.
We need to set a value for the total number that a goal needs in order to be completed.
So let's say I wanted to eat salad once a week for four weeks.
The number to complete would be four.
And I would increment it by one every time.
So we're going to call that goal completion value.
How about that goal completion value.
Because that's the value that we need in order to complete the goal.
So to save this we're going to use an integer 32.
That means it's a 32 bit integer and we're using in 32 simply because it is easier for us to access
in code and we can't set a regular integer this way.
Now we could use a double a decimal afloat.
All that stuff but 32 is what we're going to use.
And next we're going to add one final attribute.
We have a value held for the completion value but now we also need to set a value to monitor the progress.
Let's say that we have swiped and we have completed one week we would set that value to be 1.
Then we swipe again and we tap on ADD 1 then we would set it to be 2 cetera et cetera.
So we're going to call that goal progress.
Because that is the value that set to monitor the progress.
Very very cool.
And now we are done.
Our model is done we have a name.
We have four attributes.
Oops sorry forgot to set a type here.
Select 32.
And we now have our attributes totally set up.
I just want to show you how easy it is to set up this core data entity in a controller.
Now we're not going to actually use this I just want to show you how you would do it if you were going
to.
So we're going to pretend just that this is just like any other class.
We're going to import core data first and we're going to go ahead and type let goal equals and you know
what if this was a model error like if we had a swift file here if it was a class we could just type
goal.
Do you see how that pops up here.
That's so cool.
If we go ahead and give it a parentheses and we just set it to be an empty goal just like that you'll
see if I click on the parentheses here if I build it it's going to say it was never used.
That's fine.
I just need to show the color here so I can show you what's actually inside.
This looks like it's not going to let me and that's fine.
But what I can do is check this out I can type goal dot and look at this.
These are all of my properties that I just set.
So now I have an instance of my core data models model.
Like I can use this I can set the completion value to be equal to 32.
And I can pass in a value let's say passed in a double of twelve point zero.
I could set that right now I could just set my model it's so easy to actually implement this X code
Apple.
The whole team they've just made it so easy to use core data.
There's no reason for you to not be using it in your app.
A lot of people for a long time have been really hesitant to use core data just because it's been really
complicated and there's all kinds of like extra things you need to do in order to use it.
But Apple has made most of it pretty much automatic and you can use it almost interchangeably with a
normal model layer which I think actually saves you a lot of time it's really efficient and it's just
an amazing way to do it.
So we have our core data entity saved.
This is our model which as you remember from the previous video.
This is our managed object model.
Now it's going to be passed in to the ER.
It's going to be used in order to instantiate the persistent storage container.
And after that it's going to be used with the managed object context in order to pull and save and load
data from our persistent store.
So so cool guys amazing work.
And you know what.
In the next video we're going to go ahead and we're going to set up our goal cells to be displaying
in our goals Visi table view and we're also going to write an enum in order to create different gold
types.
Now we'll get back to using this goal entity a little bit later but for now let's look at our table
view set up and working.
Let's let our brain digest all of this and process all of this and we'll continue to use core data in
a few videos in the future.
Amazing job.
And let's move on.