1
00:00:08,100 --> 00:00:13,970
A guys this is Caleb with Dev slopes and in this video we're going to download images using a new function

2
00:00:13,970 --> 00:00:15,470
called retrieve images.

3
00:00:15,500 --> 00:00:20,210
We're also going to write a little function called cancel all sessions that's going to allow us to cancel

4
00:00:20,210 --> 00:00:25,650
the download session so that we don't end up having a lot of duplicate sessions.

5
00:00:25,760 --> 00:00:30,150
And so that we can properly terminate them when they need to be terminated terminated.

6
00:00:30,150 --> 00:00:31,760
Anyway let's move on.

7
00:00:31,790 --> 00:00:36,980
So we're going to open our X code project here and we're going to start by writing a function called

8
00:00:37,040 --> 00:00:38,870
retrieve images.

9
00:00:38,900 --> 00:00:43,340
So go ahead and type that phunk retrieve images.

10
00:00:43,640 --> 00:00:47,230
And the cool thing is that this function whip's retrieve images.

11
00:00:47,450 --> 00:00:52,400
This function is going to be much easier to parse through the data because we've already downloaded

12
00:00:52,400 --> 00:00:55,940
it and appended it to the image you are LRA.

13
00:00:55,940 --> 00:00:59,660
So we're just dealing with a local array of you or else all these images.

14
00:00:59,660 --> 00:01:03,190
So it's going to be much easier to use the data.

15
00:01:03,260 --> 00:01:04,180
Pretty cool.

16
00:01:04,190 --> 00:01:10,460
So let's go ahead and we need to basically create a completion handler because we need to be able to

17
00:01:10,460 --> 00:01:16,310
notify that we have downloaded all of the images and get a notice that it's done so that we can reload

18
00:01:16,310 --> 00:01:20,090
the collection view load in all the images to the cells et cetera et cetera.

19
00:01:20,120 --> 00:01:28,160
So to do that we're going to basically create an identical handler to appear by typing handler at escaping

20
00:01:29,150 --> 00:01:34,490
or making it escaping so that the value can be passed outside of this function.

21
00:01:34,640 --> 00:01:36,750
Ok so it doesn't get stuck in the closure.

22
00:01:36,950 --> 00:01:42,020
And let's go ahead and create a function that's going to return an empty function we don't really need

23
00:01:42,020 --> 00:01:45,730
to return anything and that's why we're doing that inside the function.

24
00:01:45,740 --> 00:01:49,770
We use an underscore because function types are not allowed to have names.

25
00:01:49,850 --> 00:01:51,220
So we use an underscore.

26
00:01:51,470 --> 00:01:58,040
And we're just going to use this call this the status of type Boolean in case status means finished

27
00:01:58,040 --> 00:02:00,120
or not call.

28
00:02:00,170 --> 00:02:01,460
So that's that.

29
00:02:01,490 --> 00:02:07,580
Now we need to actually first create an array that's going to hold all the images just like we did with

30
00:02:07,580 --> 00:02:09,120
image you are right.

31
00:02:09,140 --> 00:02:15,530
So let's go up to the top of our class here and let's create a variable for that as well called var

32
00:02:15,860 --> 00:02:17,700
image array.

33
00:02:18,230 --> 00:02:27,830
And let's go ahead and call that type UI image OK and let's instantiate it.

34
00:02:27,830 --> 00:02:29,840
Scroll back down.

35
00:02:29,960 --> 00:02:31,080
And where were we.

36
00:02:31,130 --> 00:02:33,300
We were right here retrieve images.

37
00:02:33,320 --> 00:02:34,200
OK.

38
00:02:34,220 --> 00:02:37,880
When we enter this function we should first clear out image array.

39
00:02:37,880 --> 00:02:43,490
So go ahead and type image array and then equals empty brackets that way if there are currently images

40
00:02:43,490 --> 00:02:47,190
in there it's cleared out so that the new ones can come in and not interrupt.

41
00:02:47,480 --> 00:02:53,000
But basically what we're going to do now is we're going to cycle through our recently filled image you

42
00:02:53,000 --> 00:03:00,050
r l array and we're going to basically create an Alamo file request to download each image from its

43
00:03:00,050 --> 00:03:00,890
you are out.

44
00:03:01,010 --> 00:03:06,380
So go ahead and type for you Arel an image u r l array.

45
00:03:06,380 --> 00:03:14,340
Then we're going to call Alamo fire request and we'll pass it in the U R L K from our array.

46
00:03:14,360 --> 00:03:16,760
Now this is where we're using Alamo fire image.

47
00:03:16,760 --> 00:03:18,090
This is the coolest thing.

48
00:03:18,110 --> 00:03:23,180
Normally Alamo fire does not have the capability to download a straight up image.

49
00:03:23,240 --> 00:03:28,340
It does have the ability to download data or we could parse through Jason and then cast that as data

50
00:03:28,370 --> 00:03:31,250
and then create a UI image pass in the data.

51
00:03:31,310 --> 00:03:32,320
All of that.

52
00:03:32,600 --> 00:03:40,950
But Alamo fire image allows us to call daat response image to see that dot response image and it returns

53
00:03:40,950 --> 00:03:43,280
a completion handler of type image.

54
00:03:43,280 --> 00:03:50,120
So press enter on that and we're going to call this response because that's the response is a data response

55
00:03:50,710 --> 00:03:52,820
of type image.

56
00:03:52,820 --> 00:03:59,060
Now what we can do inside this function is we're going to deal with the response and we're going to

57
00:03:59,240 --> 00:04:02,530
create a constant that holds the image value.

58
00:04:02,540 --> 00:04:10,640
So go ahead and type guard let image equals response that result value that might help if I spell response

59
00:04:10,640 --> 00:04:16,520
right response that result value and that pulls out the image value from the response.

60
00:04:16,520 --> 00:04:21,510
Now of course if we don't get anything else we want to just return.

61
00:04:21,560 --> 00:04:24,710
Meaning get out of this function so that we don't have a crash.

62
00:04:24,710 --> 00:04:27,390
That's a way we can be a safe coder.

63
00:04:27,410 --> 00:04:28,780
Now we need to use this image.

64
00:04:28,790 --> 00:04:32,220
So we're going to go ahead and for this particular you are.

65
00:04:32,250 --> 00:04:40,270
We're going to call a self image array append and we're going to give it the image from our response.

66
00:04:40,290 --> 00:04:46,580
Now if you go ahead and look at what this is you'll see that it's of type image.

67
00:04:46,800 --> 00:04:51,120
And if we go into image array it's of type UI image.

68
00:04:51,120 --> 00:04:57,690
Now the cool thing is when we pass it in image is a comparable type from Alamo fire that we can use

69
00:04:57,690 --> 00:05:01,020
as an image is really really cool that it lets us do that.

70
00:05:01,020 --> 00:05:08,160
So we just downloaded an image from our u r l we append it to our image array and now we need to update

71
00:05:08,190 --> 00:05:11,810
our progress label right to show that that image has been downloaded.

72
00:05:12,000 --> 00:05:20,400
So let's go ahead and type self up progress label dot text and that's going to equal blank out of 40

73
00:05:20,760 --> 00:05:23,810
images downloaded.

74
00:05:23,910 --> 00:05:31,280
Now if you remember we basically have a number that is being downloaded every time this comes through

75
00:05:31,290 --> 00:05:32,900
we want it to be updated.

76
00:05:32,910 --> 00:05:37,370
So what we can do is we can give it the number of items in the image array.

77
00:05:37,380 --> 00:05:40,420
So let's go ahead and do that by using string encapsulation.

78
00:05:40,560 --> 00:05:46,890
And let's just call a self image array count that way once we have one it will show up as one out of

79
00:05:46,890 --> 00:05:49,710
40 images downloaded then two then three than four.

80
00:05:49,890 --> 00:05:55,980
And that is how we're going to store that value so that we can see how many images have been downloaded.

81
00:05:55,980 --> 00:05:58,610
So the user has a clue of what's going on.

82
00:05:58,740 --> 00:06:00,030
That's really easy.

83
00:06:00,060 --> 00:06:06,810
Now what we're going to do beneath this is we're going to set up a check to see if the amount of images

84
00:06:07,080 --> 00:06:10,850
in our image array matches the amount of you or else we downloaded.

85
00:06:11,040 --> 00:06:17,850
So to do that what we're going to do is we're going to go ahead and type if self image array count is

86
00:06:17,940 --> 00:06:23,200
equal to self image you are LRA count.

87
00:06:23,850 --> 00:06:26,030
Then we're going to go ahead and use our handler.

88
00:06:26,040 --> 00:06:28,010
That's when we know that we're done.

89
00:06:28,170 --> 00:06:32,790
So type handler and pass it a boolean of true.

90
00:06:33,000 --> 00:06:34,570
That is how we know that we're done.

91
00:06:34,590 --> 00:06:38,200
And that's how we can tell that we have all of the images that we need.

92
00:06:38,490 --> 00:06:45,390
So let's go ahead and let's go up to where we called this originally which is in drop pin and we need

93
00:06:45,390 --> 00:06:46,850
to think what are we going to do now.

94
00:06:46,860 --> 00:06:49,120
We've retrieved all of our you ourselves.

95
00:06:49,260 --> 00:06:55,470
And we actually can get rid of this print function but now that we have retrieved them we need to retrieve

96
00:06:55,470 --> 00:06:57,130
all the images from them.

97
00:06:57,210 --> 00:06:58,540
But I see a problem.

98
00:06:58,650 --> 00:07:01,380
We passed in true here and that works.

99
00:07:01,380 --> 00:07:09,750
But the issue is that this is supposed to be a constant with its own unique name like finished K meaning

100
00:07:09,750 --> 00:07:15,690
that the value could be set as true or false but simply by passing in true here we're sort of overwriting

101
00:07:15,690 --> 00:07:17,130
this to always return true.

102
00:07:17,130 --> 00:07:18,360
But what if it failed.

103
00:07:18,420 --> 00:07:23,610
So we need to pass in finished so that we know whether or not we actually have a true or false value

104
00:07:23,620 --> 00:07:30,660
it should be able to change and we need to do a conditional check here so if finished meaning if finished

105
00:07:30,660 --> 00:07:33,550
returns as true we could also do if finished equals true.

106
00:07:33,570 --> 00:07:36,330
That's fine but you can just say if finished.

107
00:07:36,330 --> 00:07:41,950
Meaning if it's true then what we need to do is retrieve images.

108
00:07:42,000 --> 00:07:43,540
Simple as that.

109
00:07:43,620 --> 00:07:48,620
And of course we we're going to want to use the handler so that we know when that is finished.

110
00:07:48,930 --> 00:07:50,250
And let's go ahead and type that.

111
00:07:50,250 --> 00:07:53,480
So finished is what we can use for that handler.

112
00:07:53,490 --> 00:07:56,570
That's how we know that we're done downloading all of our images.

113
00:07:56,820 --> 00:08:03,180
And what do we want to do if finished what should we do.

114
00:08:03,180 --> 00:08:09,660
Well we should hide the spinner because remember the spinner is spinning on our collection.

115
00:08:09,960 --> 00:08:17,160
We should hide the label and we should also reload our collection view with all of the images that we

116
00:08:17,160 --> 00:08:18,030
just now download.

117
00:08:18,060 --> 00:08:21,930
We want them to show up right so we can do two of these things.

118
00:08:21,930 --> 00:08:25,060
Now we can hide the spinner and we can hide the label.

119
00:08:25,080 --> 00:08:33,660
So I'm going to delete those and call self dot remove spinner and self remove label or remove progress

120
00:08:33,660 --> 00:08:34,790
label rather.

121
00:08:34,830 --> 00:08:37,410
And those are the two things that we can do.

122
00:08:37,410 --> 00:08:42,690
We have not yet set up our collection view to even pull data from our image array.

123
00:08:42,810 --> 00:08:44,090
We'll do that in the next video.

124
00:08:44,250 --> 00:08:47,860
But I'm going to save this here so that we remember that that's what we're going to do.

125
00:08:47,880 --> 00:08:51,340
There's only one last issue we need to look at and that is here.

126
00:08:51,420 --> 00:08:57,090
Since we're inside of another closure we need to call self because this dysfunction is publicly accessible

127
00:08:58,320 --> 00:09:02,610
in this class and we need to call self so that we can use it from the view controller.

128
00:09:02,650 --> 00:09:08,230
So we should go ahead and check to see if this works we should get all of our images downloading.

129
00:09:08,480 --> 00:09:12,950
It should update our label it should remove our spinner and progress label when it reaches 40.

130
00:09:12,950 --> 00:09:14,510
Let's go ahead and build and run.

131
00:09:14,660 --> 00:09:15,980
Let's see how we did.

132
00:09:16,040 --> 00:09:21,020
And then in the next video we'll set up our collection view so that it actually loads the images into

133
00:09:21,050 --> 00:09:23,350
all of the different cells in the collection view.

134
00:09:23,660 --> 00:09:26,280
So let's give it a try.

135
00:09:26,320 --> 00:09:35,820
It's loading K There we go we have zoomed in let's drop a pin and let's see if the label gets updated.

136
00:09:40,470 --> 00:09:40,880
All right.

137
00:09:40,890 --> 00:09:41,550
Awesome.

138
00:09:41,550 --> 00:09:45,510
So it looks like the label is showing up it says three out of 45 out of 46 out of 40.

139
00:09:45,510 --> 00:09:45,950
OK good.

140
00:09:45,950 --> 00:09:50,880
So it's updating every time there's a photo downloading and I'm noticing that the fonts too big.

141
00:09:50,880 --> 00:09:55,320
So I'm actually going to go change that where we create the label which I think is up here near the

142
00:09:55,320 --> 00:09:58,890
top add progress label.

143
00:09:59,040 --> 00:10:00,880
Let's make it maybe size 14.

144
00:10:00,900 --> 00:10:02,870
That'll update in the next build.

145
00:10:02,880 --> 00:10:06,980
So for now we're just waiting until it gets to 40 to see if it properly hides.

146
00:10:06,990 --> 00:10:11,350
And later it will reload the collection view.

147
00:10:11,490 --> 00:10:12,000
All right.

148
00:10:12,120 --> 00:10:12,490
Awesome.

149
00:10:12,480 --> 00:10:14,450
So it looks like it made it to 40.

150
00:10:14,490 --> 00:10:15,630
It hid.

151
00:10:15,840 --> 00:10:18,170
And that's exactly what we were hoping for.

152
00:10:18,170 --> 00:10:19,020
This is awesome.

153
00:10:19,020 --> 00:10:27,060
So now we need to handle the instance of what if a user cancels What if they swipe down like we can

154
00:10:27,270 --> 00:10:30,750
to cancel it should stop the request it should stop the download.

155
00:10:30,750 --> 00:10:31,640
Right.

156
00:10:31,740 --> 00:10:35,770
And we're going to set up a function that can do that called cancel all sessions.

157
00:10:35,970 --> 00:10:41,480
So in order to get to that scroll down and we're going to put it right beneath retrieve images.

158
00:10:41,480 --> 00:10:50,280
Go ahead and type phunk cancel all sessions and then inside of those curly braces we're going to actually

159
00:10:50,310 --> 00:10:53,450
access a singleton class from Alamo fire.

160
00:10:53,460 --> 00:10:58,560
So go ahead and type Alom a fire session manager that's a singleton.

161
00:10:58,560 --> 00:11:01,380
We're going to get the default session manager.

162
00:11:01,380 --> 00:11:07,080
We're going to get it session and we're going to call a function here called Get tasks with completion

163
00:11:07,080 --> 00:11:08,370
handler.

164
00:11:08,490 --> 00:11:15,700
Now press enter to set up this completion handler and we're going to call this session data task.

165
00:11:15,920 --> 00:11:19,110
Ok this is all of the data tasks that come in.

166
00:11:19,110 --> 00:11:23,900
This is going to be called upload up load data.

167
00:11:24,090 --> 00:11:24,520
OK.

168
00:11:24,570 --> 00:11:26,880
And this is going to be download data.

169
00:11:26,880 --> 00:11:34,440
These are three constants that are used to hold all three different types of sessions that can be happening

170
00:11:34,470 --> 00:11:35,210
in your app.

171
00:11:35,370 --> 00:11:40,770
Now we're required to use all three of these here but we are not required to use them all within the

172
00:11:40,770 --> 00:11:41,520
closure.

173
00:11:41,550 --> 00:11:42,630
We're not uploading anything.

174
00:11:42,630 --> 00:11:46,020
So we actually don't need to worry about the upload data session task.

175
00:11:46,020 --> 00:11:47,520
But here's what we're going to do.

176
00:11:47,520 --> 00:11:53,160
We're going to cancel every instance of session data task and every instance of download data.

177
00:11:53,370 --> 00:11:56,520
If you look you can see that session data task.

178
00:11:56,760 --> 00:12:00,960
Maybe you can't at the moment well anyway session data task is an array.

179
00:12:00,960 --> 00:12:01,590
This is an array.

180
00:12:01,590 --> 00:12:06,300
This is an array we have arrays here meaning we're going to need to cycle through all of them to cancel

181
00:12:06,300 --> 00:12:07,500
all the tasks.

182
00:12:07,500 --> 00:12:10,630
There's a really cool and easy Swiftie way to do it.

183
00:12:10,650 --> 00:12:18,390
So let's start with the session data task by typing session data task for each and for each allows us

184
00:12:18,390 --> 00:12:24,180
basically to cycle through like a for loop but instead of saying for a task in-session data task and

185
00:12:24,180 --> 00:12:31,200
then calling tasks cancel what we can do is instead we can give it some curly braces we can use dollar

186
00:12:31,200 --> 00:12:38,640
signs zero and dollar signs zero basically creates that placeholder as if we were typing for a task

187
00:12:38,700 --> 00:12:41,600
in session data task right.

188
00:12:41,610 --> 00:12:45,220
We could do a print task or will like.

189
00:12:45,230 --> 00:12:53,160
Like I said we could do tasks cancel right now task is like a temporary variable that is used for every

190
00:12:53,160 --> 00:12:55,150
instance in that array.

191
00:12:55,350 --> 00:13:00,400
Dollar Signs zero is the same thing it's just kind of a generic variable name.

192
00:13:00,600 --> 00:13:04,500
And so we can just call DOT cancel we can fit everything on one line.

193
00:13:04,500 --> 00:13:05,850
It's like a single line for loop.

194
00:13:05,850 --> 00:13:07,170
Really really cool.

195
00:13:07,170 --> 00:13:11,880
So we just canceled all of the session data tasks and now we need to cancel all of the download data

196
00:13:11,880 --> 00:13:18,340
tasks as well so called download data for each wups for each make sure you get the right one there.

197
00:13:18,540 --> 00:13:20,880
Curly braces and then inside.

198
00:13:21,030 --> 00:13:24,310
Dollar Signs zero dot cancel.

199
00:13:24,540 --> 00:13:29,040
Now using this function we just canceled all of the sessions that we need.

200
00:13:29,040 --> 00:13:35,130
And that's actually the perfect way to make sure that you're not having multiple data sessions happening

201
00:13:35,130 --> 00:13:40,230
on top of one another that can be really really inefficient in your app and so we're going to make sure

202
00:13:40,230 --> 00:13:42,360
that our app is nice and efficient.

203
00:13:42,360 --> 00:13:44,850
And now we need to just think of where does this go.

204
00:13:44,850 --> 00:13:46,860
Cancel all sessions.

205
00:13:46,860 --> 00:13:54,260
Remember in our app when we drop a pen it pulls up our view here like that when we swipe down.

206
00:13:54,330 --> 00:13:55,280
It hides.

207
00:13:55,470 --> 00:13:59,100
I think that's where it should be cancelling every session as well.

208
00:13:59,100 --> 00:14:03,870
So we need to go find the animate down function which I believe is up here.

209
00:14:04,170 --> 00:14:04,680
Yes.

210
00:14:04,740 --> 00:14:05,210
OK.

211
00:14:05,430 --> 00:14:10,470
So animate view down is here and whenever we swipe down we should cancel all sessions.

212
00:14:10,470 --> 00:14:13,010
That's a great place to put it.

213
00:14:13,080 --> 00:14:14,520
So that's looking good.

214
00:14:14,530 --> 00:14:16,360
That'll cancel all of the sessions.

215
00:14:16,410 --> 00:14:23,640
Now there's another thing that we should do is well what if the view is up and what if we click to add

216
00:14:23,700 --> 00:14:24,300
a new pin.

217
00:14:24,300 --> 00:14:29,640
Right let's say we get to maybe 4 photos and we add a new pin.

218
00:14:29,670 --> 00:14:34,460
Believe it or not it's not actually canceling the session it's creating a new one on top of that.

219
00:14:34,470 --> 00:14:39,810
So in order to be the most efficient we should definitely remove or cancel all sessions before we start

220
00:14:39,810 --> 00:14:40,870
a new session.

221
00:14:40,920 --> 00:14:47,280
So in droppin that's that's where we double click and drop a pin we should cancel the session and started

222
00:14:47,280 --> 00:14:51,730
over so that we get a brand new fresh fresh session every time.

223
00:14:51,750 --> 00:14:52,430
Very cool.

224
00:14:52,440 --> 00:14:57,840
So let's go ahead and call cancel all sessions from drop in as well when we remove all of the stuff

225
00:14:57,840 --> 00:15:00,490
we might as well remove the session as well.

226
00:15:00,660 --> 00:15:02,940
So very very cool.

227
00:15:03,060 --> 00:15:07,720
Well sorry let's actually go check to see if this works let's build and run it before we move on.

228
00:15:08,220 --> 00:15:14,260
We'll have the the terminal or the console pop up and the console will show us when the sessions are

229
00:15:14,320 --> 00:15:14,880
exited.

230
00:15:14,950 --> 00:15:21,500
So let's go ahead and open it up here we'll get a lot of a lot of debug messages here.

231
00:15:21,550 --> 00:15:26,410
Now I'm going to go ahead and drop a new pin cancelled out and you'll see right here it says finished

232
00:15:26,410 --> 00:15:32,440
with error and error just means that it printed an error saying maybe user cancelled session or something

233
00:15:32,440 --> 00:15:33,700
like that.

234
00:15:33,970 --> 00:15:40,960
But the cool thing is if I swipe down as well like so you'll notice we get the same finished with error

235
00:15:40,960 --> 00:15:43,930
message meaning the task is being canceled and we're starting over.

236
00:15:43,930 --> 00:15:44,900
Every single time.

237
00:15:45,130 --> 00:15:46,590
Which is really really cool.

238
00:15:46,600 --> 00:15:51,650
This is a great way to make sure that our app is nice and efficient and not wasting any resources.

239
00:15:51,690 --> 00:15:55,250
So go ahead and we're going to move on to the next video.

240
00:15:55,360 --> 00:16:00,910
We're going to set up our collection view to have an image view to pass in an image to that image view

241
00:16:01,120 --> 00:16:04,150
and to add that is a sub view to our collection view cell.

242
00:16:04,150 --> 00:16:07,680
That way we can show all of our awesome images that we just downloaded from Flickr.

243
00:16:07,680 --> 00:16:10,060
I'm really excited to get that working.

244
00:16:10,060 --> 00:16:13,080
So let's go ahead and head over to the next video and let's get it set up.

