Master Swift async/await, actors, async let parallel tasks, AsyncStream, and the @MainActor attribute.
0 / 5 completed
1 / 5
What does the async keyword on a Swift function signify?
async/await: Swift's structured concurrency model. An async function can suspend at any await point, freeing the thread for other work. The runtime resumes the function on an appropriate executor when the awaited work completes, making async code read like synchronous code.
2 / 5
What is a Swift actor and what problem does it solve?
Actor:actor Counter { var value = 0; func increment() { value += 1 } }. Accessing counter.value from outside requires await, which the compiler enforces. The actor's executor ensures mutations are serialised, providing data-race safety without manual locking.
3 / 5
What does async let enable in Swift?
async let:async let image = downloadImage(url); async let data = fetchMetadata(id) launches both concurrently. let result = await (image, data) waits for both. This is structured concurrency — the tasks are scoped to the enclosing block and automatically cancelled if an error is thrown.
4 / 5
What is AsyncStream used for in Swift?
AsyncStream: you create one with a builder closure that calls continuation.yield(value) to emit values and continuation.finish() to end. Consumers iterate: for await event in stream { ... }. It bridges delegate callbacks, notifications, or any push-based source to the async/await world.
5 / 5
What is the @MainActor attribute used for in Swift?
@MainActor: annotate your SwiftUI view models or UIKit view controllers with @MainActor. The compiler enforces that all mutable property accesses and method calls happen on the main thread. Callers from other actors must await them, making UI threading safe by construction.