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