SwiftUI Concurrency with async await
One of the most important features of Swift
5.5
, introduced in iOS 15, was the introduction of the async
and await
keywords. With async
and await
, we can write asynchronous concurrent code almost as if it were synchronous code, one statement after the other.
Concurrency means that different pieces of code run at the same time. Often, we must orchestrate these pieces of code to create sequences of events to present the results in a view.
Before Swift 5.5, the most common way of creating a sequence of concurrent code was by using a completion block
. When the first part of the code finishes, we call a completion
block where we start the second piece of code. This works and is manageable if we have only two asynchronous functions to synchronize, but it would become quickly unmaintainable with multiple functions and different ways of synchronizing them. For example, we could have two asynchronous functions to wait for before starting the third...