async/await in SwiftUI
Convert a SwiftUI app to use the new Swift concurrency and find out what’s going on beneath the shiny surface. By Audrey Tam.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
async/await in SwiftUI
35 mins
- Getting Started
- Old and New Concurrency
- Pyramid of Doom
- Data Races
- Thread Explosion / Starvation
- Tasks and Continuations
- JokeService
- Minimal Error Handling
- Show Me a Joke!
- Concurrent Binding
- Awaiting async
- Sequential Binding
- Error Handling Options
- Creating an Unstructured Task
- Decoding the Joke
- MainActor
- Actor
- @MainActor
- One More Thing: Asynchronous View Modifiers
- Where to Go From Here?
- WWDC 2021 Videos
- Melbourne Cocoaheads
WWDC 2021 Videos
To learn more about the fundamental concepts of Swift concurrency, watch these videos first:
-
Meet async/await in Swift: This session introduces
try await
,get async
, unstructured task and continuations. - Explore structured concurrency in Swift: This session covers concurrent and sequential binding, cancellation, task groups, unstructured and detached tasks. There’s a handy Flavors of task table near the end.
-
Protect mutable state with Swift actors: This session covers data races, actor, protocol conformance in extensions with nonisolated declarations, detached tasks,
Sendable
, conformance andMainActor
.
These sessions are SwiftUI-specific:
- Demystify SwiftUI: Get inspired to check all your SwiftUI code for inefficiencies. Lots of cute dog and cat photos.
- Discover concurrency in SwiftUI: This session provides valuable tips for using structured concurrency in a SwiftUI app (SpacePhoto).
These talks cover a few specific purposes:
- Meet AsyncSequence
-
Use async/await with URLSession: This session covers
guard let
andthrow
, upload and download methods, cancellation,AsyncSequence
andAuthenticationDelegate
. Lots of cute dog images. - Bring Core Data concurrency to Swift and SwiftUI
Set aside a good chunk of uninterrupted time for these deeper dives, or watch them in 10-15 minute chunks:
- Swift concurrency: Update a sample app: This session is solid gold! The presenter Ben Cohen is the Review Manager of the Swift Evolution proposal SE-0304 Structured Concurrency.
- Swift concurrency: Behind the scenes: This session goes in depth about thread explosion, the cooperative thread pool, continuations, async frames.
Melbourne Cocoaheads
And finally, two notable contributions from my colleagues at Melbourne Cocoaheads.
- How to test Swift async/await code with XCTest by Giovanni Lodi. This is a companion piece to his presentation (starts at 46:32).
- CombineAsyncually by Rob Amos. This is the companion repo for his presentation (right after Gio’s, at 1:17:27), where he demonstrates how you can bridge the new async/await functionality with Combine.
I hope you enjoyed this tutorial! Swift concurrency is a huge game-changer and it’s still evolving. There’s a lot to learn, so take some time now to explore it, even if you can’t really use it for a few years.
If you have any comments or questions, feel free to join in the forum discussion below!