Swift simplified: Completion handler vs return

Adam Dev
4 min readAug 2, 2021

--

Hello readers,
Today after a very long time i am posting this article which will help you what is the completion handler and its usage and ofcourse what is the difference between the completion handler and the return

What is the completion handler?
Let us take a real time example : Suppose an employee after completing his work for the day must shut down his system or lock his system before leaving the office. And after shutting down his system he must exit by using his punching card

So here you can see there are 3 scenarios:
A) Completing the work
B) Shutting/Locking the system after completing the work
C) Exit office by using the punching card

So if we see all these scenarios are interrelated that is the employee can only shutdown his system after completing the work and he can only leave the office after shutting down the system, similarly if we want to map this concept in the swift programming we use the completion handler for this.

We use the completion handler to do a task after the interrelated pre tasks has been executed or done fully. For example if we see a simple code like : present(login, animated: true, completion: nil). Here after presenting the login view controller if we need to execute a code so instead of “nil” we can just write present(login, animated: true) {
print (“Completed”) }

How to use the completion handler?

Now lets map the above 3 scenarios in the form of the completion handler
I wrote the function with a completion handler like this:

completion handler

In the above function completion “completion: (String) -> ()” is expecting a string message after the task is done.

Now suppose i am invoking this function from viewDidLoad like the below code :

Invoking part

So here we can see that:
A) The function is passing the “Completed” status to the performTaskUsingCompletion parameter workStatus.

B)Once the Scenario 1 is printed the completion hander is getting a status of “shutting down” through completion(“Shutting down”). Since the work is completed and the message has been printed for scenario 1, scenario 2 gets printed with the “Shutting down” as passed message

C) Once the scenario 3 is printed it means the system has shut down and the employee is leaving the office by using the punching car

Lets see the output:

Output

So if we use completion handler we can execute the statement even after the completion part is executed.

Why not to use return ?

Lets see the sample function with the return type “String”. We are mapping the above 3 scenarios in the following way

Return Statement

You can see the Xcode complaints “Code after return will not be executed” . Gosh!!! so it means i will be stuck in the office itself ? Maybe i have an amnesia and i don’t remember how to use the punching card or i forgot where i kept it 🤣. Lets find out.

I invoke the above function like this:

Invoke

Output will be like this:

Output

Hmmmm only scenario 1 and scenario 2 got executed…. Output shows i have a clear case of Amnesia and i dont know how to execute the Scenario 3. I am trying to return from office just after shutting down the system. Where is my punching id??? I guess i need 👨‍⚕️👨‍⚕️ Dr. Shaun murphy from the “Good Doctor” series 😎… I need a completion handler transplant urgently!!!!!! Please clap appreciating my own diagnosis.

--

--

Adam Dev

MOBILITY ENGINEER/ARCHITECTURE,FRAMEWORK CREATOR/GITHUB OWL