mirror of
https://github.com/Dimillian/Skills.git
synced 2026-03-25 08:55:54 +00:00
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.
1.6 KiB
1.6 KiB
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
Viewis@MainActorisolated by default; members andbodyinherit isolation.- Swift 6.2 can infer
@MainActorfor all types in a module (new language mode). - This default simplifies UI code and aligns with UIKit/AppKit
@MainActorAPIs.
Where SwiftUI runs code off the main thread
- SwiftUI may evaluate some view logic on background threads for performance.
- Examples:
Shapepath generation,Layoutmethods,visualEffectclosures, andonGeometryChangeclosures. - These APIs often require
Sendableclosures to reflect their runtime semantics.
Sendable closures and data-race safety
- Accessing
@MainActorstate from aSendableclosure is unsafe and flagged by the compiler. - Prefer capturing value copies in the closure capture list (e.g., copy a
Bool). - Avoid sending
selfinto 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
Taskto 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.