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