1
00:00:08,000 --> 00:00:10,780
Hey everybody this is Caleb began with slopes dot com.

2
00:00:10,790 --> 00:00:15,460
And in this video I'm so excited we're going to create our core data model.

3
00:00:15,730 --> 00:00:22,200
This is going to allow us to create a core data entity and give it some core data attributes as well.

4
00:00:22,250 --> 00:00:28,250
And basically this is going to be our data model how we're going to store objects of type goal in our

5
00:00:28,250 --> 00:00:33,290
app and then later we'll be able to fetch them and pull them all up into our table view to display them.

6
00:00:33,380 --> 00:00:34,820
Really cool stuff.

7
00:00:34,820 --> 00:00:38,940
Let's go ahead and let's pull open our X code project and let's go set it up.

8
00:00:39,250 --> 00:00:39,600
OK.

9
00:00:39,620 --> 00:00:45,560
So to do that what we have done already is when we built this project from the beginning when we created

10
00:00:45,560 --> 00:00:53,150
it we ticked include core data so that automatically creates what's called an X C data model D and it

11
00:00:53,150 --> 00:00:59,600
is the same name as our project so I'm actually going to close the assistant editor here and this is

12
00:00:59,600 --> 00:00:59,880
it.

13
00:00:59,880 --> 00:01:01,610
This is the core data interface.

14
00:01:01,610 --> 00:01:03,170
Now there are a few things.

15
00:01:03,170 --> 00:01:05,110
Let me just show you around a little bit.

16
00:01:05,150 --> 00:01:08,110
There's a section here for entities will talk about that in a second.

17
00:01:08,240 --> 00:01:10,040
There's a section for fecche requests.

18
00:01:10,040 --> 00:01:11,770
We're not going to use this feature.

19
00:01:11,930 --> 00:01:14,330
We'll do it in code but we're not going to do it here.

20
00:01:14,570 --> 00:01:16,670
And then there are configurations.

21
00:01:16,680 --> 00:01:19,470
K now this displays your entities.

22
00:01:19,610 --> 00:01:23,510
And later we'll show you the attributes section.

23
00:01:23,540 --> 00:01:27,920
This is of course on the side is just some information and this will I will show you this isn't just

24
00:01:27,920 --> 00:01:30,780
a second as you see when I hover over it.

25
00:01:30,840 --> 00:01:35,250
It'll pop up a name here and it is the data model inspector that is.

26
00:01:35,270 --> 00:01:36,510
We'll use that in just a little bit.

27
00:01:36,530 --> 00:01:42,560
But for now let's go ahead and let's create our entity think of this just like a model layer for any

28
00:01:42,560 --> 00:01:43,890
other app that you're going to build.

29
00:01:43,910 --> 00:01:44,530
OK.

30
00:01:44,630 --> 00:01:47,920
An entity is like a model or like a class can then.

31
00:01:47,920 --> 00:01:49,660
Attributes are just like properties.

32
00:01:49,730 --> 00:01:53,800
So go ahead and click add entity and we get a blank entity.

33
00:01:53,810 --> 00:01:59,120
Now there are attributes relationships and fetched properties in this app we're only going to focus

34
00:01:59,120 --> 00:02:01,970
on attributes but relationships and fetch properties.

35
00:02:02,000 --> 00:02:06,110
I really encourage you to dive in deep and get to know what those are.

36
00:02:06,200 --> 00:02:06,710
OK.

37
00:02:06,980 --> 00:02:11,360
Now our entity we don't want to name it entity because that's just kind of a generic name.

38
00:02:11,360 --> 00:02:12,290
We're going to rename it.

39
00:02:12,290 --> 00:02:18,480
So right click or sorry click on it once and then you can basically edit the name Kay.

40
00:02:18,560 --> 00:02:22,330
So this will not be called entity but instead we're going to call it goal.

41
00:02:22,440 --> 00:02:22,700
OK.

42
00:02:22,700 --> 00:02:27,560
Think of this as if we were to make a model layer called goal we would set up some properties and we

43
00:02:27,560 --> 00:02:33,500
would create an initialiser to set those values whenever we instantiate an instance of our goal model.

44
00:02:33,500 --> 00:02:37,040
But we're going to use core data instead of doing that it's really cool.

45
00:02:37,040 --> 00:02:44,380
So we think of goal as our class and the attributes are like our properties so what does a goal need.

46
00:02:44,390 --> 00:02:47,140
Click an item and this is our first property.

47
00:02:47,150 --> 00:02:49,090
But in core data it's called an attribute.

48
00:02:49,220 --> 00:02:49,870
OK.

49
00:02:50,090 --> 00:02:53,270
So we're going to call this goal description.

50
00:02:53,630 --> 00:02:54,200
OK.

51
00:02:54,500 --> 00:02:55,420
And it's type.

52
00:02:55,430 --> 00:02:57,460
We need to set explicitly right here.

53
00:02:57,620 --> 00:03:03,350
Now of course like before the goal description is just text so we can give it a value of type string.

54
00:03:03,350 --> 00:03:09,140
You'll notice on the side here in the data model inspector you can see that we can give it a name we

55
00:03:09,140 --> 00:03:11,960
can set some properties here the attribute type.

56
00:03:11,960 --> 00:03:17,090
And there's all kinds of complex validation and a default value we can set.

57
00:03:17,150 --> 00:03:19,510
There's all kinds of complex stuff that you can set in here.

58
00:03:19,520 --> 00:03:23,030
But for now we just need to give a name and said it's type.

59
00:03:23,150 --> 00:03:24,090
So let's keep going.

60
00:03:24,140 --> 00:03:30,000
Let's go ahead and set a goal type and that is also going to be string.

61
00:03:30,170 --> 00:03:30,950
OK.

62
00:03:30,950 --> 00:03:34,870
Now that's for short term or long term which of course is textual data.

63
00:03:34,880 --> 00:03:38,810
So we need to use a string came very cool.

64
00:03:38,810 --> 00:03:42,710
Next we're going to go ahead and create a new attribute.

65
00:03:43,110 --> 00:03:46,530
We're just we're still thinking of these like properties in a class.

66
00:03:46,670 --> 00:03:47,260
OK.

67
00:03:47,510 --> 00:03:53,470
We need to set a value for the total number that a goal needs in order to be completed.

68
00:03:53,470 --> 00:03:56,890
So let's say I wanted to eat salad once a week for four weeks.

69
00:03:56,990 --> 00:03:59,020
The number to complete would be four.

70
00:03:59,030 --> 00:04:02,420
And I would increment it by one every time.

71
00:04:02,510 --> 00:04:09,150
So we're going to call that goal completion value.

72
00:04:09,230 --> 00:04:10,780
How about that goal completion value.

73
00:04:10,790 --> 00:04:14,100
Because that's the value that we need in order to complete the goal.

74
00:04:14,180 --> 00:04:18,320
So to save this we're going to use an integer 32.

75
00:04:18,320 --> 00:04:24,890
That means it's a 32 bit integer and we're using in 32 simply because it is easier for us to access

76
00:04:24,890 --> 00:04:28,660
in code and we can't set a regular integer this way.

77
00:04:28,670 --> 00:04:31,300
Now we could use a double a decimal afloat.

78
00:04:31,310 --> 00:04:34,170
All that stuff but 32 is what we're going to use.

79
00:04:34,310 --> 00:04:37,270
And next we're going to add one final attribute.

80
00:04:37,520 --> 00:04:45,890
We have a value held for the completion value but now we also need to set a value to monitor the progress.

81
00:04:45,890 --> 00:04:51,280
Let's say that we have swiped and we have completed one week we would set that value to be 1.

82
00:04:51,290 --> 00:04:56,070
Then we swipe again and we tap on ADD 1 then we would set it to be 2 cetera et cetera.

83
00:04:56,090 --> 00:04:59,750
So we're going to call that goal progress.

84
00:04:59,840 --> 00:05:03,350
Because that is the value that set to monitor the progress.

85
00:05:03,350 --> 00:05:04,430
Very very cool.

86
00:05:04,610 --> 00:05:06,610
And now we are done.

87
00:05:06,740 --> 00:05:08,490
Our model is done we have a name.

88
00:05:08,630 --> 00:05:09,780
We have four attributes.

89
00:05:09,800 --> 00:05:11,930
Oops sorry forgot to set a type here.

90
00:05:11,930 --> 00:05:13,400
Select 32.

91
00:05:13,790 --> 00:05:17,680
And we now have our attributes totally set up.

92
00:05:17,840 --> 00:05:22,490
I just want to show you how easy it is to set up this core data entity in a controller.

93
00:05:22,490 --> 00:05:26,090
Now we're not going to actually use this I just want to show you how you would do it if you were going

94
00:05:26,090 --> 00:05:27,250
to.

95
00:05:27,440 --> 00:05:31,310
So we're going to pretend just that this is just like any other class.

96
00:05:31,430 --> 00:05:37,820
We're going to import core data first and we're going to go ahead and type let goal equals and you know

97
00:05:37,820 --> 00:05:43,040
what if this was a model error like if we had a swift file here if it was a class we could just type

98
00:05:43,130 --> 00:05:44,030
goal.

99
00:05:44,110 --> 00:05:45,680
Do you see how that pops up here.

100
00:05:45,740 --> 00:05:47,000
That's so cool.

101
00:05:47,000 --> 00:05:52,640
If we go ahead and give it a parentheses and we just set it to be an empty goal just like that you'll

102
00:05:52,640 --> 00:05:57,830
see if I click on the parentheses here if I build it it's going to say it was never used.

103
00:05:57,920 --> 00:05:58,910
That's fine.

104
00:05:58,910 --> 00:06:03,330
I just need to show the color here so I can show you what's actually inside.

105
00:06:03,330 --> 00:06:06,860
This looks like it's not going to let me and that's fine.

106
00:06:06,860 --> 00:06:10,970
But what I can do is check this out I can type goal dot and look at this.

107
00:06:10,970 --> 00:06:13,620
These are all of my properties that I just set.

108
00:06:13,640 --> 00:06:18,210
So now I have an instance of my core data models model.

109
00:06:18,230 --> 00:06:22,530
Like I can use this I can set the completion value to be equal to 32.

110
00:06:23,090 --> 00:06:29,730
And I can pass in a value let's say passed in a double of twelve point zero.

111
00:06:29,990 --> 00:06:35,480
I could set that right now I could just set my model it's so easy to actually implement this X code

112
00:06:35,720 --> 00:06:36,410
Apple.

113
00:06:36,440 --> 00:06:39,670
The whole team they've just made it so easy to use core data.

114
00:06:39,680 --> 00:06:42,100
There's no reason for you to not be using it in your app.

115
00:06:42,230 --> 00:06:47,600
A lot of people for a long time have been really hesitant to use core data just because it's been really

116
00:06:47,600 --> 00:06:51,750
complicated and there's all kinds of like extra things you need to do in order to use it.

117
00:06:51,800 --> 00:06:58,100
But Apple has made most of it pretty much automatic and you can use it almost interchangeably with a

118
00:06:58,100 --> 00:07:03,320
normal model layer which I think actually saves you a lot of time it's really efficient and it's just

119
00:07:03,320 --> 00:07:04,860
an amazing way to do it.

120
00:07:04,880 --> 00:07:08,910
So we have our core data entity saved.

121
00:07:09,290 --> 00:07:13,150
This is our model which as you remember from the previous video.

122
00:07:13,280 --> 00:07:14,990
This is our managed object model.

123
00:07:15,020 --> 00:07:17,260
Now it's going to be passed in to the ER.

124
00:07:17,300 --> 00:07:21,310
It's going to be used in order to instantiate the persistent storage container.

125
00:07:21,590 --> 00:07:27,710
And after that it's going to be used with the managed object context in order to pull and save and load

126
00:07:27,710 --> 00:07:29,920
data from our persistent store.

127
00:07:29,960 --> 00:07:32,670
So so cool guys amazing work.

128
00:07:32,780 --> 00:07:33,250
And you know what.

129
00:07:33,250 --> 00:07:37,910
In the next video we're going to go ahead and we're going to set up our goal cells to be displaying

130
00:07:37,910 --> 00:07:43,940
in our goals Visi table view and we're also going to write an enum in order to create different gold

131
00:07:43,940 --> 00:07:44,630
types.

132
00:07:44,630 --> 00:07:49,040
Now we'll get back to using this goal entity a little bit later but for now let's look at our table

133
00:07:49,040 --> 00:07:50,400
view set up and working.

134
00:07:50,450 --> 00:07:55,430
Let's let our brain digest all of this and process all of this and we'll continue to use core data in

135
00:07:55,430 --> 00:07:56,900
a few videos in the future.

136
00:07:56,900 --> 00:07:57,790
Amazing job.

137
00:07:57,800 --> 00:07:59,010
And let's move on.

