Swift Concurrency
Priority: P0
Implementation Guidelines
async/await
- •Async Functions: Mark with
async, call withawait. - •Error Handling: Combine with
throwsfor async throwing functions. - •No Completion Handlers: Prefer
asyncover callback-based APIs.
Actors
- •Data Isolation: Use
actorfor mutable state accessed from multiple tasks. - •MainActor: Annotate UI code with
@MainActorfor main thread execution. - •Actor Isolation: All actor properties/methods are isolated automatically.
Task Management
- •Structured Concurrency: Use
Task {},async let,TaskGroup. - •Cancellation: Check
Task.isCancelled, propagate cancellation. - •Detached Tasks: Avoid
Task.detachedunless necessary.
Anti-Patterns
- •Blocking Main Thread:
**No synchronous work in @MainActor**: Use Task. - •Missing MainActor:
**UI updates must be @MainActor**: Compiler error. - •Ignoring Cancellation:
**Check Task.isCancelled**: Respect cancellation.