Async/Await in Swift5.5 – Walkthrough in Few minutes

Async/Await in Swift is the most interesting addition in Swift 5.5, with this addition our concurrency programming in Swift is getting improved a lot. In this article, let’s see how concurrency programming is without using async await and with using async await in Swift in this article. I promise, you will be amazed yourself after understanding Swift’s async await.

devCracker
3 min readNov 1, 2021

This is not a new concept in programming world, many programming languages like java script and others has this async await programming pattern already. Let’s begin to see, how it helps Swift developer’s concurrency programming.

To understand the concept, we can take a use case where we are getting the data from a server and saving the data locally into coredata.

In this use case, we have two asynchronous process, 1. getting the data from backend 2. storing the data locally.

Swift async await — use case without using async await

This use case is done without any issues with completion block, but there are some downside.

  1. Boilerplate code and completion block doom is growing bigger when we add more logic or scaling the use case.
  2. There is a high chance that we might miss to call the completion block which leads the caller to wait endlessly.
  3. May be, little more effort is needed to understand the logic for new developers.

How async await helps asynchronous programming here:

With Swift’s async await our code comes linear like below,

Swift async await — use case with async await
Swift async await — use case with async await

How cool it is, no more afraid of forgetting the completion block and calling completion block from many cases. It’s pretty straight.

Await in Swift:

You might have wondered what is await here in the above snapshot. Yes, await always comes together with async, they both cant live without helping each other. Meaning that async tells that asynchronous process is happening and await informs that I am awaiting for the result.

‘async’ call in a function that does not support concurrency:

As soon as we start using the async method in a synchronous method, compiler throws this error and we have to make this method also as async method.

Swift async await — compiler error while using in a method
Swift async await — compiler error while using in a method

But this compiler suggestion “to make the method as async” will go on in a chain till the root. This would make us to add async everywhere, where actually it does not need to be. To solve this, we can create a Task inside the method as below.

Swift async await — Task in concurrency programming
Swift async await — Task in concurrency programming

Take away from here:

How cool this new Swift async await addition is, few take away or recap from this article.

  1. Making our code more readable.
  2. No more completion handler with pyramid pattern logic.
  3. Easier to maintain and scalable.

Please follow/subscribe me if you like my post. I would encourage to read about Task, Task Group in Swift to get more knowledge on Swift’s concurrency programming.

Upgrade your medium membership from here to read good amount of article and scale your skills.

My another Interesting post, well it may be 💁, depends your interest and opinion:

My another interesting Post which i assume it would solve some of our iOS developer community’s pain at-least for time being. Please checkout my article to prevent or secure your app from screenshot and recordings.

--

--

devCracker

professional mobile app developer | iOS | Android | GraphQL | Typescript | nodejs. Sports and musics fuels my mind to get back to development