A guys this is Caleb with Dev slopes and in this video we're going to download images using a new function
called retrieve images.
We're also going to write a little function called cancel all sessions that's going to allow us to cancel
the download session so that we don't end up having a lot of duplicate sessions.
And so that we can properly terminate them when they need to be terminated terminated.
Anyway let's move on.
So we're going to open our X code project here and we're going to start by writing a function called
retrieve images.
So go ahead and type that phunk retrieve images.
And the cool thing is that this function whip's retrieve images.
This function is going to be much easier to parse through the data because we've already downloaded
it and appended it to the image you are LRA.
So we're just dealing with a local array of you or else all these images.
So it's going to be much easier to use the data.
Pretty cool.
So let's go ahead and we need to basically create a completion handler because we need to be able to
notify that we have downloaded all of the images and get a notice that it's done so that we can reload
the collection view load in all the images to the cells et cetera et cetera.
So to do that we're going to basically create an identical handler to appear by typing handler at escaping
or making it escaping so that the value can be passed outside of this function.
Ok so it doesn't get stuck in the closure.
And let's go ahead and create a function that's going to return an empty function we don't really need
to return anything and that's why we're doing that inside the function.
We use an underscore because function types are not allowed to have names.
So we use an underscore.
And we're just going to use this call this the status of type Boolean in case status means finished
or not call.
So that's that.
Now we need to actually first create an array that's going to hold all the images just like we did with
image you are right.
So let's go up to the top of our class here and let's create a variable for that as well called var
image array.
And let's go ahead and call that type UI image OK and let's instantiate it.
Scroll back down.
And where were we.
We were right here retrieve images.
OK.
When we enter this function we should first clear out image array.
So go ahead and type image array and then equals empty brackets that way if there are currently images
in there it's cleared out so that the new ones can come in and not interrupt.
But basically what we're going to do now is we're going to cycle through our recently filled image you
r l array and we're going to basically create an Alamo file request to download each image from its
you are out.
So go ahead and type for you Arel an image u r l array.
Then we're going to call Alamo fire request and we'll pass it in the U R L K from our array.
Now this is where we're using Alamo fire image.
This is the coolest thing.
Normally Alamo fire does not have the capability to download a straight up image.
It does have the ability to download data or we could parse through Jason and then cast that as data
and then create a UI image pass in the data.
All of that.
But Alamo fire image allows us to call daat response image to see that dot response image and it returns
a completion handler of type image.
So press enter on that and we're going to call this response because that's the response is a data response
of type image.
Now what we can do inside this function is we're going to deal with the response and we're going to
create a constant that holds the image value.
So go ahead and type guard let image equals response that result value that might help if I spell response
right response that result value and that pulls out the image value from the response.
Now of course if we don't get anything else we want to just return.
Meaning get out of this function so that we don't have a crash.
That's a way we can be a safe coder.
Now we need to use this image.
So we're going to go ahead and for this particular you are.
We're going to call a self image array append and we're going to give it the image from our response.
Now if you go ahead and look at what this is you'll see that it's of type image.
And if we go into image array it's of type UI image.
Now the cool thing is when we pass it in image is a comparable type from Alamo fire that we can use
as an image is really really cool that it lets us do that.
So we just downloaded an image from our u r l we append it to our image array and now we need to update
our progress label right to show that that image has been downloaded.
So let's go ahead and type self up progress label dot text and that's going to equal blank out of 40
images downloaded.
Now if you remember we basically have a number that is being downloaded every time this comes through
we want it to be updated.
So what we can do is we can give it the number of items in the image array.
So let's go ahead and do that by using string encapsulation.
And let's just call a self image array count that way once we have one it will show up as one out of
40 images downloaded then two then three than four.
And that is how we're going to store that value so that we can see how many images have been downloaded.
So the user has a clue of what's going on.
That's really easy.
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
in our image array matches the amount of you or else we downloaded.
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
equal to self image you are LRA count.
Then we're going to go ahead and use our handler.
That's when we know that we're done.
So type handler and pass it a boolean of true.
That is how we know that we're done.
And that's how we can tell that we have all of the images that we need.
So let's go ahead and let's go up to where we called this originally which is in drop pin and we need
to think what are we going to do now.
We've retrieved all of our you ourselves.
And we actually can get rid of this print function but now that we have retrieved them we need to retrieve
all the images from them.
But I see a problem.
We passed in true here and that works.
But the issue is that this is supposed to be a constant with its own unique name like finished K meaning
that the value could be set as true or false but simply by passing in true here we're sort of overwriting
this to always return true.
But what if it failed.
So we need to pass in finished so that we know whether or not we actually have a true or false value
it should be able to change and we need to do a conditional check here so if finished meaning if finished
returns as true we could also do if finished equals true.
That's fine but you can just say if finished.
Meaning if it's true then what we need to do is retrieve images.
Simple as that.
And of course we we're going to want to use the handler so that we know when that is finished.
And let's go ahead and type that.
So finished is what we can use for that handler.
That's how we know that we're done downloading all of our images.
And what do we want to do if finished what should we do.
Well we should hide the spinner because remember the spinner is spinning on our collection.
We should hide the label and we should also reload our collection view with all of the images that we
just now download.
We want them to show up right so we can do two of these things.
Now we can hide the spinner and we can hide the label.
So I'm going to delete those and call self dot remove spinner and self remove label or remove progress
label rather.
And those are the two things that we can do.
We have not yet set up our collection view to even pull data from our image array.
We'll do that in the next video.
But I'm going to save this here so that we remember that that's what we're going to do.
There's only one last issue we need to look at and that is here.
Since we're inside of another closure we need to call self because this dysfunction is publicly accessible
in this class and we need to call self so that we can use it from the view controller.
So we should go ahead and check to see if this works we should get all of our images downloading.
It should update our label it should remove our spinner and progress label when it reaches 40.
Let's go ahead and build and run.
Let's see how we did.
And then in the next video we'll set up our collection view so that it actually loads the images into
all of the different cells in the collection view.
So let's give it a try.
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.
All right.
Awesome.
So it looks like the label is showing up it says three out of 45 out of 46 out of 40.
OK good.
So it's updating every time there's a photo downloading and I'm noticing that the fonts too big.
So I'm actually going to go change that where we create the label which I think is up here near the
top add progress label.
Let's make it maybe size 14.
That'll update in the next build.
So for now we're just waiting until it gets to 40 to see if it properly hides.
And later it will reload the collection view.
All right.
Awesome.
So it looks like it made it to 40.
It hid.
And that's exactly what we were hoping for.
This is awesome.
So now we need to handle the instance of what if a user cancels What if they swipe down like we can
to cancel it should stop the request it should stop the download.
Right.
And we're going to set up a function that can do that called cancel all sessions.
So in order to get to that scroll down and we're going to put it right beneath retrieve images.
Go ahead and type phunk cancel all sessions and then inside of those curly braces we're going to actually
access a singleton class from Alamo fire.
So go ahead and type Alom a fire session manager that's a singleton.
We're going to get the default session manager.
We're going to get it session and we're going to call a function here called Get tasks with completion
handler.
Now press enter to set up this completion handler and we're going to call this session data task.
Ok this is all of the data tasks that come in.
This is going to be called upload up load data.
OK.
And this is going to be download data.
These are three constants that are used to hold all three different types of sessions that can be happening
in your app.
Now we're required to use all three of these here but we are not required to use them all within the
closure.
We're not uploading anything.
So we actually don't need to worry about the upload data session task.
But here's what we're going to do.
We're going to cancel every instance of session data task and every instance of download data.
If you look you can see that session data task.
Maybe you can't at the moment well anyway session data task is an array.
This is an array.
This is an array we have arrays here meaning we're going to need to cycle through all of them to cancel
all the tasks.
There's a really cool and easy Swiftie way to do it.
So let's start with the session data task by typing session data task for each and for each allows us
basically to cycle through like a for loop but instead of saying for a task in-session data task and
then calling tasks cancel what we can do is instead we can give it some curly braces we can use dollar
signs zero and dollar signs zero basically creates that placeholder as if we were typing for a task
in session data task right.
We could do a print task or will like.
Like I said we could do tasks cancel right now task is like a temporary variable that is used for every
instance in that array.
Dollar Signs zero is the same thing it's just kind of a generic variable name.
And so we can just call DOT cancel we can fit everything on one line.
It's like a single line for loop.
Really really cool.
So we just canceled all of the session data tasks and now we need to cancel all of the download data
tasks as well so called download data for each wups for each make sure you get the right one there.
Curly braces and then inside.
Dollar Signs zero dot cancel.
Now using this function we just canceled all of the sessions that we need.
And that's actually the perfect way to make sure that you're not having multiple data sessions happening
on top of one another that can be really really inefficient in your app and so we're going to make sure
that our app is nice and efficient.
And now we need to just think of where does this go.
Cancel all sessions.
Remember in our app when we drop a pen it pulls up our view here like that when we swipe down.
It hides.
I think that's where it should be cancelling every session as well.
So we need to go find the animate down function which I believe is up here.
Yes.
OK.
So animate view down is here and whenever we swipe down we should cancel all sessions.
That's a great place to put it.
So that's looking good.
That'll cancel all of the sessions.
Now there's another thing that we should do is well what if the view is up and what if we click to add
a new pin.
Right let's say we get to maybe 4 photos and we add a new pin.
Believe it or not it's not actually canceling the session it's creating a new one on top of that.
So in order to be the most efficient we should definitely remove or cancel all sessions before we start
a new session.
So in droppin that's that's where we double click and drop a pin we should cancel the session and started
over so that we get a brand new fresh fresh session every time.
Very cool.
So let's go ahead and call cancel all sessions from drop in as well when we remove all of the stuff
we might as well remove the session as well.
So very very cool.
Well sorry let's actually go check to see if this works let's build and run it before we move on.
We'll have the the terminal or the console pop up and the console will show us when the sessions are
exited.
So let's go ahead and open it up here we'll get a lot of a lot of debug messages here.
Now I'm going to go ahead and drop a new pin cancelled out and you'll see right here it says finished
with error and error just means that it printed an error saying maybe user cancelled session or something
like that.
But the cool thing is if I swipe down as well like so you'll notice we get the same finished with error
message meaning the task is being canceled and we're starting over.
Every single time.
Which is really really cool.
This is a great way to make sure that our app is nice and efficient and not wasting any resources.
So go ahead and we're going to move on to the next video.
We're going to set up our collection view to have an image view to pass in an image to that image view
and to add that is a sub view to our collection view cell.
That way we can show all of our awesome images that we just downloaded from Flickr.
I'm really excited to get that working.
So let's go ahead and head over to the next video and let's get it set up.