Completion Handlers in Swift 5.1

What's changed with completion blocks and their syntax?

Anand Nimje
Sep 26, 2018 · 4 min read
Photo by La-Rel Easter on Unsplash

We spend most of our time dealing with the web API and other asynchronous tasks inside our projects. In this piece on getting started with completion handlers (block syntax) in Swift, I’ll show you a better way that uses the help of blocks.

We have seen this code so many times:

URLSession methods

Let’s see it after writing code:

URLSession code for URL request

In the above code, we are calling a web URL with the help of URLSession. How do we handle this code after getting a response inside this block? If we have to pass it or return it in another function without using any global variable and other function, how we can do that?


Let’s See How To Create Blocks

Write your completion block:

This is how it will return our data in this function after executing operations in the main task:

Your code will look like this when you will call the function:


Try It With Other Things and Make It More Advanced

ImageProvider class, for example, using here for Completionhandler. It will download the image asynchronously from the image URL.

We call the function for downloading images with the help of other request functions by using the CompletionHandler. In the code below, we can see how it will work for us:


What Is @escaping and @nonescaping CompletionHandler?

If you have seen my code where I have used loadImages, you’ll have seen that inside the function block type is escaping. After Swift 3.0, blocks (in Swift closures) are non-escaping by default.

So what is the main difference between these? And what about performing any asynchronous tasks inside the function, like URLSession , or using GCD blocks for executing any operations, storing offline data in SQL or core data?

In this case, it will show you the error so that you can fix it. It will not accept a non-escaping type block. You have to fix it by using an escaping block.

@escaping closure

Conclusion

In this piece, you got an understanding of how to create your completion handler block syntax in Swift. This makes it pretty easy to perform asynchronous code inside your project. Now you have an understanding of how to manage the function with return type with the use of blocks, and, after that, how to use it inside your code. This will make your work easier. You can make any methods or wrapper with the help of block syntax. For example, inside your project, to perform web API calling and store database operations, you can make the wrapper.

I have also covered advanced uses of completionHandler. Now you understand escaping and non-escaping blocks.


Thanks for reading! I hope you enjoyed this piece. Do you have any queries regarding this tutorial, or do you know a simpler way or have anything extra to add? Please let me know via questions, feedback, or comments.

Better Programming

Advice for programmers.

Anand Nimje

Written by

I am Swift developer and makes iOS apps

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade