Swift concurrency uses async/await, Task, and actors for structured parallelism—compare with coroutines in Kotlin and goroutines in Go.
async/await basics
func fetchValue() async -> Int {
try? await Task.sleep(nanoseconds: 50_000_000)
return 42
}
Async functions suspend without blocking threads— the runtime resumes when work completes.
Tasks and actors
Task { } starts async work; actor types serialize access to mutable state—preventing data races at compile time where isolation rules apply.
Important interview questions and answers
- Q: async vs DispatchQueue?
A: async/await is structured and compiler-checked; GCD remains under the hood but async APIs reduce callback pyramids. - Q: MainActor?
A: Annotates UI-bound code to run on the main thread—critical for SwiftUI updates locally.
Self-check
- What keyword marks a suspending function?
- Why use an actor instead of a class with a lock?
Tip: Use @MainActor for UI updates in real apps—async lessons use short Task.sleep demos.
Interview prep
- async vs GCD?
async/await structured and compiler-checked; GCD under the hood.
- MainActor?
Runs UI code on main thread—critical for SwiftUI.