Completion Handler : Swift

Ekramul Hoque
Good Morning Swift
Published in
3 min readJun 22, 2018

Execute When Complete Other Task.

Introduction:

When i was started learn swift i was pretty much confuse about some concept completion handler is one of them. In this article i will explain what is completion handler and how it work and why we need it .

What need to know before learn Completion Handler :

For the clear understand of completion handler we need to know basic of closure ,trilling closure , Little bit about synchronous and asynchronous function. You can chat my published article on closure is here .

Why We Need Completion handler :

In a function if we need to do any task after finish another task Than how we make it happen .Like we will update UI after complete the download how can we make it happen ???? In other word how can we make ordered execution ? all this type problem solve completion hander concept .

Make a Completion handler :

Create a completion handler closure and than pass it to function.

Here we create an completion handler closure which type is () → Void .Now We will create a function which can take this closure ..

Here we create a function which take a parameter of type () →Void .Now implement this by calling function like this ..

So it must be execute takeCompletehandler body statement first than execute compilation handler . console will be like .

From the function BodyFrom The Compleation handler!

Lets give another Example :

lets make an function which downloading data from server and after complete download its give and notification to user .

Create download Function first .

This function can take a closure and a bool argument when when download is done completion handler parameter take true value and give notification .Now lets create and completion handler closure which will take bool value and return nothing …

lets implement function

So it should print downloading data from server .. 100 times than print “Complete download Data”

//Print this 
downloading data from sarver....
downloading data from sarver....downloading data from sarver....downloading data from sarver....downloading data from sarver....
......
Complete download data

Pass Closure Directly as a completion handler :

We know we can write closure directly on function parameter so lets implement this ..

This also print same and completion handler will execute after finish downloading .. and give notification …

Short hand

We can write it very short hand also like this :

Here we don’t use parameter instead of using swift default parameter.

Now if we use trilling closure we can use like this also

So finally we are clear that complain handler is nothing more than passing a closure in function and make it execute when other stuff done .

Pass Data by Completion handler :

We can pass data by completion handler .

First Create a function which can take closure of type ([String]) → Void

Lets implement this

Now Console should be print “Do Some Work” 10 times than print completion handler array ..

Do Some workDo Some workDo Some workDo Some work
......
.........
//10 time this
FinishDaily Swift

Very Shorthand using trilling closure and default parameter ..

passingData {print("passed data \($0[0])")}

Using typealias :

make typealias first

typealias compleatuionhandler = ([String])->Void

Lets create function

Lets implement with trilling closure :

it should print downloading file 10 times and than execute completion handler .

downloading filedownloading filedownloading filedownloading filework done of complete compleation handler

Conclusion :So hope its clear that how completion handler work and how .Thank your for staying with me .If its help you dont forget to share to clap to me .

About me : I am swift lover and iOS Developer

My Linked in : https://www.linkedin.com/in/ekramulhoque/

--

--