The Curious Case of the Async Cafe


An Introduction to Modern Concurrency in Swift

The Curious Case of the Async Cafe

Swift's solution to asynchronous and concurrent code is now part of the language itself. The new syntax is easier to read and follow the logical flow of what in the past was complicated, fragmented, or deeply nested. With async/await you'll focus on tasks instead of threads or even queues.

"The Curious Case of the Async Cafe" is a quirky fast-paced tour of the components of modern concurrency in Swift including async, await, Tasks, using continuations to replace and wrap delegates and closure-based APIs, AsyncSequences and AsyncStreams, structured concurrency using async let and Task Groups, and, of course, actors.

Updated for Xcode 14 and Swift 5.7 and tested on Apple Silicon.

Contents


Chapter 1: Async, Await, and Task

We Begin

Errors

The (too) Big Sleep

Introducing async / await

Task

Async and Errors

Testing

Be Careful

Chapter 2: Continuations

The Delegate Pattern

Delegate to Continuations

Multiple Continuations

Unsafe Continuations

Throwing Continuations

Closures and Callbacks

Async Alternatives

Async Wrappers

Refactorings

URLSession and Closures with Errors

Chapter 3: AsyncSequences and AsyncStreams

Notifications

Introducing AsyncStream

Sequences of Notifications

Sendable and Actor Boundaries

Transforming AsyncSequences

Sequence Pipelines

Combine

AsyncStream Continuations

Continuous Delivery

AsyncAlgorithms

Chapter 4: Structured Concurrency

Unstructured Async Calls

Async Let

Cancelling Tasks

Task Values

Unstructured App Store Search

TaskGroups

Sequences of TaskGroups

AsyncImage

Chapter 5: Actors

On and Off the Main Actor

Shared Mutable State

Actors

Suspending and Reentrancy

Nonisolated Members

Communication between Actors

Global Actors

TaskLocal

Creating Distributed Actors

Using Distributed Actors

Epilogue

Related


Async Training from Dim Sum Thinking

Screenshots


screenshot 1

screenshot 2

screenshot 3