Swift Concurrency for Beginners: Getting Started with Async/Await
Ever since Swift 5.5 we have had access to fun concurrency features in Swift.
I think there is a crucial part of this, and it’s Swift’s
async/await syntax. This article is intended to provide a beginner friendly introduction to this syntax and explain how it might be used in a practical way.
Before we start
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 14.2, and Swift 5.7.2
- You will be expected to be aware of how to make a Single View Application
- Some familiarity with GCD and concurrency would be useful for the reader
Keywords and Terminology:
Asynchronous: Work that can be run out of order, and usually have a callback when completed.
async/await: An alternative to completion handlers, a coroutine model that allows execution to be suspended and resumed.
I’ve already written a simple example article about async/await. I think it’s time to go further into this topic and think about how we might use async/await in our projects.
Asynchronous Programming: What Is It?
Concurrency is a crucial aspect of modern programming which allows developers to perform multiple tasks efficiently.
The concurrency features available since Swift 5.5 simplify asynchronous programming for iOS developers.
Completing multiple tasks while avoiding blocking the main thread? That would mean that your App would remain responsive even when performing network requests (or complex computations).
Callbacks and Completion Handlers
I’ve created a SwiftUI project that calls
https://api.chucknorris.io/jokes/random. This code uses completion handlers in order to handle asynchronous tasks (in this case making the network call). It is difficult to reason about this code, and if we were introduce further…