Photo by Ethan Hu on Unsplash
  • Swift concurrency means one piece of the program executes at a time.
  • The function uses @escaping completion block to return the result asynchronously.
  • We write our network functions with a completion handler to get the result in the closure whenever we call this it works perfectly but there are few checks we have to do (error handling, conditional unwrapping, and decoding.)
  • This find it very difficult to you what is happening in the closure after a time. It is hard to read and understand the flow.
  • For this in Swift 5.5 apple introduced the async/await mechanism which is used to write asynchronous code in a structured way or you can say like a normal code. It suspends the asynchronous code and resumes it later and only one piece of the program executes at a time.
  • async/await function allows us to write asynchronous code in a single line.
  • Isn’t it AWESOME. 🤩
  • Lets look at it.
  • First, we will see the example with the completion handler how it is then we will write that in the async/await function.
  • Fetching live news with completion block, which is everyone is familiar to this and it works fine.
Returning news using completion block
  • If you see it is hard to read and debug.
  • A lot of error handling for to just fetch news and ended up with 25 lines of code, a little bit scary.

Lets see how we call this function:-

Calling fetchNews function and got the news in completion block closure.

Now we will see how we can do this using async/await and how we can optimise this.

Async Keyword

  • Async function is an asynchronous function in which you can write asynchronous code in a straight line. and suspend the execution part of code like network calls.
  • Mark async keyword to asynchronous function return and just before the throws, so it will return data or throws the error.

Await keyword

  • Await indicate where the function might unblock the thread.
  • You can mark await keyword in the front of the function and it will suspend until it will return data/error.
  • we use await on URLSession Data because data from is an async function and you want to wait for its execution.

How we call the async function

Calling async fetchNews function and handling error using do-catch
  • The fetchNews() function is returning throws that’s why we have to use do-catch for to handle errors.
  • This is safer and shorter.
  • Easy to understand, debug and handle errors are become easy.
Using async closure called liveNews function and render the UI on main thread
  • I’m returning news from liveNews() function and rendered news on UI in async closure.
  • Another option is you can add DispatchQueue.main.async {} in live news function after getting the news from fetchNews() function and render the news on UI.

I have created multiple async functions to get to know our main thread is blocking or not and on which queue async functions are executing.

First async looping function
Second async looping function
  • In the above image, I have added breakpoint in two async functions to get to know on which queue are they running.
Result
  • In the above image, after we got the result from async functions, the main thread is here to render the data.

What other things in the async/await

  • We saw async function, but we can write async properties also.
  • Async properties can be only read-only properties.
  • You can use await in the for loop also.

Lets look at it how it will be created.

Async properties

Async properties
  • This is an async variable, you have to mark async after getter and use await before calling fetchNews function.
  • I told you above read-only properties can be only async so it cannot have a setter.
  • In swift 5.5 getter property can mark also throws.

Async let

Async let
  • To call the asynchronous function and run parallel we can use async let.
  • Await and async let allow other codes to run while they are suspended.
  • You can create many child tasks and execute parallel.

Lets go through quick recap.

  • async enables a function to suspend the asynchronous code until it returns the result.
  • When the async function is suspended main thread is not blocked, so we can do schedule other work.
  • When the async function resumes it results in return from the async function come back to the original function and excution continues where it left.
  • Accessing the resource must be done asynchronously.
  • Swift concurrency model is safe just like eliminates null pointer mistakes with optionals.
  • This is how we coordinate between concurrent tasks safely.

iOS Developer