Concurrency in modern Swift with Async & Await in SwiftUI — Part 1

DevTechie
DevTechie
Published in
3 min readDec 18, 2022

--

Concurrency in modern Swift with Async & Await in SwiftUI — Part 1

Apple introduced async and await keywords with the release of Swift 5.5 to modernize the concurrency APIs for Apple ecosystems.

Before Swift 5.5, Grand Central Dispatch (GCD), Operation Queues and Completion blocks were the preferred way to manage and create a sequence of concurrent code. With the introduction of async and await, we don’t have to deal with unmanagable nested completion blocks anymore.

Async and await make code much more readable. The idea is that an asynchronous function must be decorated with the async keyword and the calling function must add the await before calling the asynchronous function.

SwiftUI supports calling asynchronous functions out of the box with Task structure and task modifier.

In this article, we will explore async and await.

Code Execution

By default code executes line after line unless specified by the developer to distribute the work on different threads, and we can see this with an easy example.

struct DevTechieAsyncAwaitExample: View {

var body: some View {
NavigationStack {
VStack {
Text("Code")
}
.onAppear {
print("Let's work on something")

--

--