How Completion Handlers Work

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello dear welcome back the pure Swift my name is James Ali so in this video I want to discuss completion hammers like indie developers completion handlers actually confused me a lot when I first started writing iOS applications I would implement them but I wouldn't really know what's going on under the hood so what I'm gonna do in this video is basically show you both side of completion handlers what's going on inside the function and what's going on as you implement a completion handler yourself I think this two-sided perspective will give you a clearer picture of what's going on and it'll make understanding completion handlers more or less easy so without further ado let's get this started so let's start by taking a look at completion handlers on the outside so it usually starts with a function who performs a particular task now once that task is done a completion handler is called going back to the task briefly I want to say that this task of the ending it could be a computation a network request or an animation but the key thing to remember is this whenever that task is done that's when the completion handler is called so now that we take a look at completion handlers from the outside let's see how do you work inside the function let's have an Xcode now let's start by looking at what's going on inside the function and then we're gonna take a look at what's going on when we call the function I really think we're most explanations lack is that they don't show what's going on on both end so let's get started I created a some constant to help this tutorial get faster so let's start by creating a function we're gonna call it rotate in scale and it's going to take a view parameter which is a type of UI view and it's also going to have another parameter called completion which is may be of type closure that takes in nothing and returns nothing so far so good okay so what do we want done well the task here is to transform the view that we pass in inside the function so we could say view that transform okay is equal to an affine transform and we're going to rotate it using the constant okay and we're also going to scale it using the constants we have set up okay there we go so now we could say that the task is done more or less the task was to transform the view so since the task is done we'd call our completion Handler so notice that we're calling this parameter here we're calling this closure parameter which we call completion now this is the example this is an example where no data is passed into the completion handler where you also have a common occurrence where data is passed inside the completion handler so let's create another function called fetch data and fetch the is going to have a parameter called URL and a parameter of type closure called completion and it's going to take in a data and it's going to return nothing now let's set up our operation here we could create our URL using the URL passed we should probably put that in a guard statement to that it's it's more safe so far so good let's create a request using the URL we got and now we could create our network operation because you could use our URL session ok and there we go all right so we're in business so let's check if there's an error and if you want to be verbose you could print the localized description what's going on here I forgot to let there we go all right so you could print the localized description here all right so now we're in a success blog okay so let's grab the data so we did say the task is done because we we fetch the data and this is where we call a completion Handler again but this time we pass it the data and you're gonna see the compiler complain a little bit that is because since the network request is a asynchronous operation the closure parameter or the completion Handler needs to have an active scaping syntax right in front of this parameter this is only when you're having an asynchronous operation you're gonna need this so now that you see an instance where a function calls the completion Handler but does not pass data into it and you see another instance where a function calls the completion handler but pass the data from the TAS into it let's call you know our function and see the other side of completion handlers and you see what's going on under the hood now let's see what's happening when we call them so I'm gonna call the rotate and scale function and let me create a view for it let's see K is equal to UI view all right so we're gonna pass this read view we just created and this is you know where it gets interesting you have a closure parameter the completion Handler is a closer parameter that means you could pass functionalities into it you could pass code into it so in this instance I could say well print the task is done right but I could have passed a number of code into it I'm just keeping it simple so you get the concept across the point is this since the completion handler is a closure parameter you could pass code into it and this code that you write here is what will be performed when a completion handler is called here right since you know each time you call the function you could pass in different values so I'm gonna call rotate and scale again and I'm gonna pass the same view I could have passed a different to you but I'm gonna pass the same view but this time in a completion hammer I'm gonna do something completely different I could say read view dot transform is equal I didn't you know return it back to its normal state I could call the function another time again rotate in scale where are you okay and this time I could see read view room from super view right the point is this AMA since the completion handler is a parameter that takes in a closure and closures you could pass functionalities into it you could pass whatever custom immuno functionalities you want the closure to perform and whatever custom functionality you want the quarter to perform it will be called after the task is done and in this situation here where the completion handler has a dealer I could pretty much you know use that data to store it in an array or use that data to load a profile picture as a completion hammer I could use that data as I want when I call the function but you get the point um you know I think the key to remember here is that completion handlers are basically a closure which is called inside the function that closure is usually called after some tasks is complete usually the task you know can pass data into it but it doesn't have to so I hope this explanation help you understand completion handlers if you like the priest please press the thumbs up button and don't forget to subscribe for pure Swift bye
Info
Channel: James Allan
Views: 2,735
Rating: undefined out of 5
Keywords: Completion handlers, How completion handlers work, Understanding completion handlers, Pure Swift, James Allan, James Allan Pure Swift, iOS programming
Id: W-enNbisNNk
Channel Id: undefined
Length: 9min 6sec (546 seconds)
Published: Mon May 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.