Add SwiftUI concurrency reference and update SKILL.md

Added a new reference file summarizing SwiftUI-specific concurrency guidance from WWDC, covering actor isolation, Sendable closures, and async work structuring. Updated SKILL.md to include a link to this new reference for easier access to SwiftUI concurrency best practices.
This commit is contained in:
Thomas Ricouard 2025-12-30 17:09:50 +01:00
parent d66b7c104e
commit 5c35a6c9ee
2 changed files with 34 additions and 0 deletions

View file

@ -32,3 +32,4 @@ Common fixes:
## Reference material
- See `references/swift-6-2-concurrency.md` for Swift 6.2 changes, patterns, and examples.
- See `references/swiftui-concurrency-tour-wwdc.md` for SwiftUI-specific concurrency guidance.

View file

@ -0,0 +1,33 @@
# SwiftUI Concurrency Tour (Summary)
Context: SwiftUI-focused concurrency overview covering actor isolation, Sendable closures, and how SwiftUI runs work off the main thread.
## Main-actor default in SwiftUI
- `View` is `@MainActor` isolated by default; members and `body` inherit isolation.
- Swift 6.2 can infer `@MainActor` for all types in a module (new language mode).
- This default simplifies UI code and aligns with UIKit/AppKit `@MainActor` APIs.
## Where SwiftUI runs code off the main thread
- SwiftUI may evaluate some view logic on background threads for performance.
- Examples: `Shape` path generation, `Layout` methods, `visualEffect` closures, and `onGeometryChange` closures.
- These APIs often require `Sendable` closures to reflect their runtime semantics.
## Sendable closures and data-race safety
- Accessing `@MainActor` state from a `Sendable` closure is unsafe and flagged by the compiler.
- Prefer capturing value copies in the closure capture list (e.g., copy a `Bool`).
- Avoid sending `self` into a sendable closure just to read a single property.
## Structuring async work with SwiftUI
- SwiftUI action callbacks are synchronous so UI updates (like loading states) can be immediate.
- Use `Task` to bridge into async contexts; keep async bodies minimal.
- Use state as the boundary: async work updates model/state; UI reacts synchronously.
## Performance-driven concurrency
- Offload expensive work from the main actor to avoid hitches.
- Keep time-sensitive UI logic (animations, gesture responses) synchronous.
- Separate UI code from long-running async work to improve responsiveness and testability.