1
00:00:08,070 --> 00:00:13,200
Everybody this is Caleb with Deb's slopes and in this video we're going to use our managed object context

2
00:00:13,200 --> 00:00:18,850
to create fair request that's going to pull data that we've saved in our persistent store.

3
00:00:19,020 --> 00:00:20,480
Really really cool stuff.

4
00:00:20,610 --> 00:00:25,560
Then we're going to fill our array or we're going to create an array fill it with those goals and then

5
00:00:25,560 --> 00:00:29,540
later on we're going to use that to actually pull in that data to our table view.

6
00:00:29,550 --> 00:00:37,830
So go ahead and pull open your X code project load up goals VC and now we're going to go ahead and create

7
00:00:37,830 --> 00:00:41,440
functions that are going to fetch all of our gold data.

8
00:00:41,520 --> 00:00:43,110
Super super cool.

9
00:00:43,320 --> 00:00:44,600
Let's go ahead and do that now.

10
00:00:44,610 --> 00:00:52,890
So we're going to actually create an extension of goals Visi like so by typing extension go VC or goals

11
00:00:52,920 --> 00:00:53,660
VC.

12
00:00:53,850 --> 00:00:56,360
And inside here we're going to write three functions.

13
00:00:56,400 --> 00:00:59,090
Now we've got to make sure that we have core data important.

14
00:00:59,130 --> 00:00:59,620
We do.

15
00:00:59,670 --> 00:01:01,160
So that's a good thing.

16
00:01:01,170 --> 00:01:03,720
Let's go ahead and let's write this function.

17
00:01:03,720 --> 00:01:08,670
It's going to fetch all of our gold data and then save it into an array which we're going to create

18
00:01:08,670 --> 00:01:09,660
and maybe two minutes.

19
00:01:09,660 --> 00:01:15,810
So go ahead and type phunk fetch and we're going to go ahead and create a completion handler just like

20
00:01:15,810 --> 00:01:18,630
we did with save so that we know when we're done.

21
00:01:18,660 --> 00:01:23,680
So go ahead and type completion and the completion is going to happen inside of here.

22
00:01:23,800 --> 00:01:24,390
OK.

23
00:01:24,690 --> 00:01:30,180
And what we're going to do is we're first going to set a constant here called complete and it's going

24
00:01:30,180 --> 00:01:36,720
to be of type Boolean and this closure which is a function is going to basically return to us an empty

25
00:01:36,720 --> 00:01:40,280
function so that we can just pass in a value to here.

26
00:01:40,500 --> 00:01:46,050
And then when we call fetch we get a value return to here and then we can do something inside of the

27
00:01:46,050 --> 00:01:47,330
closure completion.

28
00:01:47,610 --> 00:01:48,870
So really cool.

29
00:01:48,990 --> 00:01:55,000
Here's what we got to do in order to get that data from our managed object context.

30
00:01:55,110 --> 00:02:00,270
We're going to basically first create a constant to hold the managed object context.

31
00:02:00,450 --> 00:02:06,390
We're going to create a fecche request which is a part of grabbing data from the persistent storage

32
00:02:06,390 --> 00:02:07,190
coordinator.

33
00:02:07,290 --> 00:02:12,780
Then we're going to basically use our managed object context to fetch all that data through the fetch

34
00:02:12,780 --> 00:02:15,800
request and then set our array full of goals.

35
00:02:15,810 --> 00:02:16,410
OK.

36
00:02:16,830 --> 00:02:17,640
So let's do that.

37
00:02:17,640 --> 00:02:30,120
Let's go ahead and type guard let managed context equals app delegate dot persistent container view

38
00:02:30,120 --> 00:02:31,570
context.

39
00:02:31,710 --> 00:02:35,040
If we don't have that else we'll just return.

40
00:02:35,310 --> 00:02:41,370
OK so assuming that we have our managed object context which more than likely we will we're going to

41
00:02:41,370 --> 00:02:43,680
move on and create what's called a fetch request.

42
00:02:43,680 --> 00:02:45,320
So let's go ahead and name that.

43
00:02:45,450 --> 00:02:51,320
Let fecche request equals and s fecche request.

44
00:02:51,480 --> 00:02:55,980
And what we're going to do is we're going to set a type of fecche request meaning what should we be

45
00:02:56,010 --> 00:02:56,990
fetching.

46
00:02:57,120 --> 00:03:02,790
Now this is going to be of type and as fecche request and if we put a parentheses we can see that we

47
00:03:02,790 --> 00:03:05,220
can call an entity name.

48
00:03:05,220 --> 00:03:10,820
Now the interesting thing is if we go to our X-C data model our entity is named goal.

49
00:03:10,830 --> 00:03:14,640
So if we go in here we can pass a string of goal.

50
00:03:14,730 --> 00:03:23,580
So now it knows that we are trying to fetch items that are of this particular entity very very cool.

51
00:03:23,580 --> 00:03:30,870
Now what we need to do is we need to call managed context fetch and pass it the fetch request but do

52
00:03:30,870 --> 00:03:33,250
you notice that it throws.

53
00:03:33,330 --> 00:03:34,770
That's our clue that we need to use.

54
00:03:34,770 --> 00:03:36,680
Do try and catch.

55
00:03:36,720 --> 00:03:42,780
So we'll just enter this and we'll pass it the fetch request but we need to call a do try catch block.

56
00:03:42,780 --> 00:03:44,650
So go ahead and type do.

57
00:03:45,000 --> 00:03:50,120
And then down below we might as well just type catch because that's where we'll handle errors.

58
00:03:50,400 --> 00:03:52,800
And now we're going to go ahead and paste this in here.

59
00:03:53,100 --> 00:03:54,990
Managed context not fetch.

60
00:03:54,990 --> 00:03:58,360
But like I said we need to go ahead and call try.

61
00:03:58,710 --> 00:04:06,120
Now do try and catch our what we use in order to do things that can throw errors and if they do we're

62
00:04:06,120 --> 00:04:14,080
going to use Debug print debug print apparently I can't type Debug.

63
00:04:14,100 --> 00:04:14,900
Print.

64
00:04:14,910 --> 00:04:15,630
There we go.

65
00:04:15,810 --> 00:04:24,160
And we're going to go ahead and print could not fetch and we'll pass the exact error from our.

66
00:04:24,210 --> 00:04:30,180
Do try and we'll give it the localized description there so that we know exactly what it's talking about.

67
00:04:30,180 --> 00:04:31,640
Very cool.

68
00:04:31,680 --> 00:04:40,290
And so this is great but I want you to notice for this function fetch if you let's call it again fetch

69
00:04:40,320 --> 00:04:44,540
you'll notice that it returns to us an array of N.S. fetch request result.

70
00:04:44,550 --> 00:04:51,120
So if we pass in our fetch requests like that it's going to return to us an array of whatever request

71
00:04:51,120 --> 00:04:53,940
we asked for which in this case is goal.

72
00:04:53,940 --> 00:04:59,820
So if this function returns to us an array of all of the results that it gets from its request we should

73
00:04:59,820 --> 00:05:05,280
create an array of goals and then set this to fetch and pass those into our array.

74
00:05:05,280 --> 00:05:06,780
So let's go up to the top.

75
00:05:07,130 --> 00:05:09,970
Beneath our table view let's go ahead and create an array.

76
00:05:10,080 --> 00:05:17,930
So type var goals which is an array of type goal OK that's easy enough and we're going to just instantiate

77
00:05:17,930 --> 00:05:19,660
it from the get go to be empty.

78
00:05:19,910 --> 00:05:21,620
OK that's easy enough.

79
00:05:21,620 --> 00:05:27,590
But down at the bottom when we get all of our managed context fetcher Quest results we're going to go

80
00:05:27,590 --> 00:05:33,000
ahead and set goals to be equal to this because the try here.

81
00:05:33,080 --> 00:05:35,410
Goals try.

82
00:05:35,480 --> 00:05:41,660
Here is going to basically call fetch return an array of the results which we know are going to be of

83
00:05:41,660 --> 00:05:42,410
type goal.

84
00:05:42,440 --> 00:05:45,000
So we can append them directly to our goals array.

85
00:05:45,120 --> 00:05:46,610
Super duper cool.

86
00:05:46,760 --> 00:05:52,730
And now that that's done once we successfully do that we can actually call our completion handler so

87
00:05:52,730 --> 00:05:54,700
we can say hey we're done.

88
00:05:55,040 --> 00:06:02,540
And you know while we're here we may as well print successfully fetched data.

89
00:06:03,130 --> 00:06:03,990
OK.

90
00:06:04,550 --> 00:06:09,770
If there is an error we're going to say could not fetch and will call completion false.

91
00:06:10,090 --> 00:06:13,190
OK so we're using our completion handler there that's great.

92
00:06:13,190 --> 00:06:15,310
And now here's what we're going to do.

93
00:06:15,440 --> 00:06:20,240
We need to think about when should we be fetching data when should that happen.

94
00:06:20,240 --> 00:06:26,360
Now if you did load is great but it's only called once when the view originally loads and if we have

95
00:06:26,360 --> 00:06:29,640
presented our create goal VC are finished.

96
00:06:29,650 --> 00:06:35,540
VC if we've presented those on the top and we dismiss them feuded load is not called because we're just

97
00:06:35,540 --> 00:06:38,320
presenting a view on top of it and dismissing it.

98
00:06:38,450 --> 00:06:46,370
What we can do is we can put this in view will appear because when the view appears that is because

99
00:06:46,370 --> 00:06:52,010
whether the app loads initially whether on a screen is presented and then dismissed view will appear

100
00:06:52,010 --> 00:06:55,850
is going to be called every single time the view appears not just when it loads.

101
00:06:55,850 --> 00:07:02,000
So we're going to call view will appear because that is actually the perfect place to put this call

102
00:07:02,000 --> 00:07:05,160
super view will appear and pass animated.

103
00:07:05,540 --> 00:07:11,210
So now we're going to go ahead and call self not fetch and we're going to pass it a completion handler

104
00:07:11,210 --> 00:07:11,650
here.

105
00:07:11,660 --> 00:07:18,860
And let's just say complete and if it's complete We're going to go ahead and do a check to see whether

106
00:07:18,860 --> 00:07:24,650
or not we should show or hide our table view because if we fetch all of our data but we get zero results

107
00:07:24,650 --> 00:07:28,200
we still should not show our table view remember that it's hidden from default.

108
00:07:28,430 --> 00:07:34,350
So we're going to go ahead and see if goals count is greater than or equal to one meaning if there's

109
00:07:34,370 --> 00:07:40,730
at least one we're going to go ahead and say table view is hidden equals false meaning table view is

110
00:07:40,730 --> 00:07:45,610
showing if there's more than one result that's brought back our table is going to be showing.

111
00:07:46,040 --> 00:07:52,970
But if that's not true else we're going to go ahead and say table view is hidden equals true cause if

112
00:07:52,970 --> 00:07:56,380
nothing is returned our table you should definitely not be showing.

113
00:07:56,660 --> 00:07:58,190
Kate that's pretty easy.

114
00:07:58,190 --> 00:08:04,190
So when the view appears it'll fetch the data it'll load the table view and it'll show it or hide it

115
00:08:04,190 --> 00:08:05,970
if it needs to really easy.

116
00:08:06,230 --> 00:08:07,340
So that's awesome.

117
00:08:07,340 --> 00:08:11,270
But now we need to go ahead and think about our table view.

118
00:08:11,420 --> 00:08:17,030
It's not going to be able to load the appropriate information into the table view because we have it

119
00:08:17,030 --> 00:08:20,990
set here to only load a static three cells.

120
00:08:20,990 --> 00:08:26,250
So in order to get the proper count for goals we're going to actually pass it goals count.

121
00:08:26,330 --> 00:08:29,430
If we had four items we would have four rows Okay.

122
00:08:29,720 --> 00:08:31,070
Very cool.

123
00:08:31,070 --> 00:08:35,720
The same thing goes for the cells we're not properly setting up the cells here because we're not properly

124
00:08:35,720 --> 00:08:37,130
accessing the goals.

125
00:08:37,280 --> 00:08:43,160
So we need to go ahead and do that by creating an instance of goal lead goal and we're going to go ahead

126
00:08:43,190 --> 00:08:47,100
and pull out each goal at the index path for each cell.

127
00:08:47,360 --> 00:08:53,970
So we're going to go into the goals array and we're going to pull out the item at the index path daro.

128
00:08:53,990 --> 00:08:57,480
And if you've ever worked with a table view and models you understand how this works.

129
00:08:57,590 --> 00:09:03,420
If the index path is one it's going to pull out the item at index Cathro one that's super easy.

130
00:09:03,470 --> 00:09:09,250
And as you can tell goal is going to be of type goal because it's coming from Arkell's Right.

131
00:09:10,370 --> 00:09:16,220
Now what we're going to do is we're going to pass in all the information we need for each cell we passed

132
00:09:16,220 --> 00:09:17,840
in a static title here.

133
00:09:17,960 --> 00:09:22,710
But what we're going to do now is actually type goal dot goal description.

134
00:09:22,880 --> 00:09:23,200
Okay.

135
00:09:23,210 --> 00:09:27,860
And that's where we're going to actually get the data from that specific goal the type we're going to

136
00:09:27,860 --> 00:09:31,280
get from gold gold type.

137
00:09:31,290 --> 00:09:32,610
All right.

138
00:09:32,960 --> 00:09:34,640
The progress amount we're going to take from.

139
00:09:34,640 --> 00:09:35,720
Goal.

140
00:09:35,780 --> 00:09:44,940
Goal progress or let's say goal progress amount because that doesn't initially have a value let's think.

141
00:09:45,860 --> 00:09:51,110
Well no you know what it needs to show the goal progress because the goal completion value is actually

142
00:09:51,110 --> 00:09:51,500
saved.

143
00:09:51,500 --> 00:09:55,360
When we create the goal the progress values always 0 from the beginning.

144
00:09:55,490 --> 00:10:00,150
So it should start at zero because as we add one little increment the number that makes way more sense.

145
00:10:00,350 --> 00:10:01,670
So very cool.

146
00:10:01,670 --> 00:10:02,990
That looks awesome.

147
00:10:03,170 --> 00:10:08,350
And we have now officially configured the cell phone our goals array I think we should build and run

148
00:10:08,350 --> 00:10:09,520
it to see if it works.

149
00:10:09,560 --> 00:10:11,140
OK let's build and run.

150
00:10:11,140 --> 00:10:15,270
Let's see if our fetch function is going to successfully work.

151
00:10:15,280 --> 00:10:17,670
And it looks like we're getting some errors here.

152
00:10:17,980 --> 00:10:19,090
Interesting.

153
00:10:19,090 --> 00:10:21,790
So let's go ahead and find out what the deal is.

154
00:10:21,880 --> 00:10:22,790
We get one error here.

155
00:10:22,810 --> 00:10:24,900
Oh of course it is not unwrapped.

156
00:10:24,940 --> 00:10:28,670
We have an optional type of string from goal description so unwrap that.

157
00:10:28,750 --> 00:10:33,120
Now it's saying that generic parameter result type could not be inferred.

158
00:10:33,530 --> 00:10:37,500
OK so we just need to set an explicit type for this as fecche request.

159
00:10:37,540 --> 00:10:43,740
We're telling it to fetch goals but we need to actually tell it that we explicitly need the type goal

160
00:10:44,580 --> 00:10:44,890
like.

161
00:10:44,980 --> 00:10:51,430
So by putting it in the carrot's there that tells it that we're asking for the type goal specifically.

162
00:10:51,430 --> 00:10:52,650
So that's great.

163
00:10:52,650 --> 00:10:57,280
All right so it's giving us an air here cannot convert value of type string to expected argument type

164
00:10:57,370 --> 00:10:59,580
gold type construct gold type.

165
00:10:59,580 --> 00:11:00,860
We don't want to do that.

166
00:11:00,910 --> 00:11:04,720
We don't want to make it into a gold type when you want to pass in the string value.

167
00:11:04,780 --> 00:11:05,130
You know what.

168
00:11:05,130 --> 00:11:11,440
I almost wonder if it would be easier if we went into gold sell and instead of passing in these values

169
00:11:11,440 --> 00:11:18,880
like this what if we just passed in a goal of type goal and instead of passing in a specific description

170
00:11:18,880 --> 00:11:27,370
value we could just say goal Daco description we could say goal dot type K because it comes in as a

171
00:11:27,370 --> 00:11:27,810
string.

172
00:11:27,830 --> 00:11:28,630
That's better.

173
00:11:28,810 --> 00:11:35,170
And then for this one we could go ahead and type goal dot goal progress.

174
00:11:35,170 --> 00:11:41,760
Now I think we're still going to have to put that into a string like so remember it's string describing.

175
00:11:42,140 --> 00:11:44,270
OK so that should still work OK.

176
00:11:44,350 --> 00:11:48,830
Nothing's wrong here but we are definitely going to have an error here.

177
00:11:48,880 --> 00:11:54,220
Now it's going to be easier on this end because configure cell now just asks for a goal and so we'll

178
00:11:54,220 --> 00:11:55,810
pass it that goal.

179
00:11:55,810 --> 00:11:57,840
And now we shouldn't get any errors.

180
00:11:57,850 --> 00:12:00,340
We should be able to properly fix that.

181
00:12:00,340 --> 00:12:02,590
Let's go look at gold type just to make sure.

182
00:12:02,640 --> 00:12:07,780
Or go sell I mean yeah because gold type comes in from our bowl is a string because when we save it

183
00:12:07,780 --> 00:12:09,460
we save it as a string.

184
00:12:09,460 --> 00:12:09,950
Awesome.

185
00:12:10,060 --> 00:12:11,540
So very very cool.

186
00:12:11,650 --> 00:12:14,160
Let's build and run this let's see if it works.

187
00:12:14,240 --> 00:12:16,410
And let's check it out.

188
00:12:17,110 --> 00:12:17,600
OK.

189
00:12:17,680 --> 00:12:18,250
Hey look at that.

190
00:12:18,250 --> 00:12:21,280
This is actually these are the ones that we've already saved.

191
00:12:21,310 --> 00:12:23,230
Let's try saving another one.

192
00:12:23,230 --> 00:12:28,710
Dance twice today just in case you feel like getting funky.

193
00:12:28,720 --> 00:12:29,370
All right.

194
00:12:29,420 --> 00:12:32,820
Press Next how many points until complete let's say three.

195
00:12:33,100 --> 00:12:38,860
And if we did this properly when Goles VC appears meaning when this dismisses it should fetch all of

196
00:12:38,860 --> 00:12:46,720
our data and it should set our table view to be showing or hiding so it's saying that it successfully

197
00:12:46,720 --> 00:12:47,460
fetch the data.

198
00:12:47,470 --> 00:12:48,990
But I'm not seeing it updating here.

199
00:12:49,000 --> 00:12:50,000
That's interesting.

200
00:12:50,020 --> 00:12:54,520
I wonder if we were to build and run it again if it would show up because I think that we're not yet

201
00:12:54,520 --> 00:12:56,430
reloading our table view like we need to.

202
00:12:56,440 --> 00:12:56,920
OK.

203
00:12:57,070 --> 00:12:57,720
Yes we'll look at that.

204
00:12:57,720 --> 00:13:01,030
So it's there but we just need to reload our table view.

205
00:13:01,450 --> 00:13:05,630
And you know what I think let's think so.

206
00:13:05,650 --> 00:13:08,040
If goals don't count as greater than 1.

207
00:13:08,050 --> 00:13:10,180
We want the table view to be showing.

208
00:13:10,180 --> 00:13:14,190
And you know what that would also be a great place to reload the table view.

209
00:13:14,660 --> 00:13:15,140
OK.

210
00:13:15,310 --> 00:13:16,270
Well no you know what.

211
00:13:16,270 --> 00:13:17,980
Because what if there's less than one.

212
00:13:17,980 --> 00:13:22,990
We also need to reload the data because if one were to be deleted.

213
00:13:23,500 --> 00:13:28,930
So you know instead of putting it here how about every time the view appears we're going to reload the

214
00:13:28,930 --> 00:13:31,840
table view then we fetch the data or no you know what.

215
00:13:31,840 --> 00:13:37,900
Even better let's fetch the data and then at the very end we will reload the table view no matter what.

216
00:13:37,900 --> 00:13:43,770
So that whether it's hidden or showing if there's more than 1 or 0 it'll reload every time.

217
00:13:44,080 --> 00:13:45,920
So let's try this out.

218
00:13:46,090 --> 00:13:54,460
Let's create a goal go to the park twice a week and let's do that for three weeks.

219
00:13:54,490 --> 00:13:56,260
Create goal.

220
00:13:56,260 --> 00:13:56,860
There we go.

221
00:13:56,860 --> 00:13:57,100
OK.

222
00:13:57,100 --> 00:13:58,190
So it shows up.

223
00:13:58,510 --> 00:13:59,330
Awesome.

224
00:13:59,620 --> 00:14:05,700
I am noticing a problem we are having some issues here with the text labels being out of place.

225
00:14:05,860 --> 00:14:07,490
So let's go fix that really quick.

226
00:14:07,490 --> 00:14:10,980
In main story board that does not look pretty.

227
00:14:11,250 --> 00:14:14,380
Ok so yeah it's probably an issue with our stack view here.

228
00:14:14,560 --> 00:14:15,480
And you know what.

229
00:14:15,580 --> 00:14:19,240
Let's see so goal is set this is set.

230
00:14:19,330 --> 00:14:26,650
And how are we filling this filling proportionally in case that's going to be a problem if we have smaller

231
00:14:26,800 --> 00:14:27,610
text.

232
00:14:27,730 --> 00:14:33,130
So let's go ahead and let's fix this so that it's not going to move anywhere.

233
00:14:33,160 --> 00:14:38,140
What we're going to do is we're going to change this to fill and fill and then what we're going to do

234
00:14:38,140 --> 00:14:44,140
is we're going to set a goal to have an explicit with so that this always is going to be the same distance

235
00:14:44,140 --> 00:14:45,030
from goal.

236
00:14:45,040 --> 00:14:51,240
So with the goal label selected click the pin menu select with and we're going to make this 50 y.

237
00:14:51,250 --> 00:14:53,260
Let's see if that works ok.

238
00:14:53,470 --> 00:14:53,860
Awesome.

239
00:14:53,860 --> 00:14:58,930
So let's go ahead let's build and run this let's see if that fixed our problem and we should be good

240
00:14:58,930 --> 00:14:59,600
to go.

241
00:14:59,620 --> 00:15:03,610
Guys we are loading data from core data we're pulling it from the ones that we've saved.

242
00:15:03,760 --> 00:15:04,600
Oh yeah look at that.

243
00:15:04,600 --> 00:15:05,890
That looks way better.

244
00:15:05,920 --> 00:15:11,430
We did mess up this one at the bottom but we can fix that by also giving type an explicit with let's

245
00:15:11,430 --> 00:15:15,300
just make it 40 and then let's build and run again.

246
00:15:15,310 --> 00:15:16,630
Let's see if that fixed our problem.

247
00:15:16,660 --> 00:15:21,500
It should unless we're not actually using the right stack.

248
00:15:21,520 --> 00:15:22,400
Ok no look at that.

249
00:15:22,420 --> 00:15:23,360
That looks perfect.

250
00:15:23,380 --> 00:15:26,990
So we're pulling the data we've saved long term short term titles.

251
00:15:27,210 --> 00:15:31,840
The the completion everything is showing exactly as we want.

252
00:15:31,840 --> 00:15:34,240
This is amazing guys awesome awesome work.

253
00:15:34,240 --> 00:15:35,560
We filled an array.

254
00:15:35,560 --> 00:15:40,720
We filled our table view with data from that array and then the next video we're going to write a function

255
00:15:40,720 --> 00:15:46,650
that's going to allow us to remove objects from core data and allow us to swipe and delete them.

256
00:15:46,810 --> 00:15:50,230
Then we're going to go ahead and add a really nice animation.

257
00:15:50,230 --> 00:15:55,100
So when you press delete it animates up no matter which row you choose to delete it's going to be super

258
00:15:55,100 --> 00:15:56,000
cool.

259
00:15:56,050 --> 00:15:59,890
And let's go build it now guys we're so close this is amazing.

260
00:15:59,890 --> 00:16:01,270
Let's move on to the next video.

261
00:16:01,270 --> 00:16:02,500
Amazing work with this one.

262
00:16:02,500 --> 00:16:03,490
I'll see you there.
