Swift's modern concurrency model — async/await, actors, and structured concurrency — was introduced in Swift 5.5 and is now the standard for concurrent iOS and macOS development. These exercises cover the key concepts and patterns for writing safe concurrent Swift code.
0 / 5 completed
1 / 5
At standup, a colleague asks what async/await enables in Swift compared to completion handlers. What is the correct answer?
async/await in Swift lets you write asynchronous code that reads like synchronous code. Marking a function async allows it to await other async functions, which suspends the current task (freeing the thread) until the awaited operation completes. This eliminates callback pyramids, makes error handling with try/catch natural, and integrates with Swift's structured concurrency system. The thread is freed during suspension — not blocked.
2 / 5
During a PR review, a teammate asks what Swift actors provide. Which answer is correct?
Swift actors protect mutable state from concurrent access. An actor's executor serialises access — only one task runs inside the actor at a time. All calls to actor methods or properties from outside the actor are implicitly async, suspending until the actor is free. This eliminates data races on actor state without explicit NSLock or DispatchQueue serialisation, and the Swift compiler enforces actor isolation at compile time.
3 / 5
In a design review, the team discusses task groups in Swift. A junior engineer asks what they enable. What is correct?
Task groups (withTaskGroup / withThrowingTaskGroup) enable structured concurrent fan-out in Swift. Inside the group closure, you call group.addTask { } to spawn child tasks that run concurrently. The group waits for all children before returning. If any child throws (in a throwing group), the group cancels remaining children and rethrows. Results are collected via for await result in group { }. This is the idiomatic way to run N concurrent tasks and aggregate results.
4 / 5
An incident report shows UI updates crashing with a main-thread violation. A senior engineer asks what @MainActor does. What is correct?
@MainActor is a global actor that ties code to the main thread. Annotating a class (e.g., your ViewModel) or specific methods with @MainActor tells the Swift compiler to schedule those executions on the main thread's executor. The compiler enforces this — calling a @MainActor method from a background task requires await, which hops to the main thread. This replaces fragile manual DispatchQueue.main.async { } calls for UI updates.
5 / 5
During a code review, a senior engineer asks what structured concurrency guarantees in Swift's task hierarchy. What is accurate?
Swift's structured concurrency creates a task hierarchy with strong guarantees. Child tasks (created via async let, TaskGroup.addTask, or within an actor) cannot outlive their parent. If the parent task is cancelled, cancellation propagates to all children. If a child throws in a throwing task group, remaining siblings are cancelled. The parent always awaits all children before returning. This prevents task leaks, makes error propagation predictable, and simplifies reasoning about concurrent lifetime.