8.11 · The order of events in a render, and what Swift 6 isolation means for views

Effects in SwiftUI hang off view lifetime and value changes, not off a single “did load”. Knowing the order and the actor each closure runs on removes a whole class of races.

Mental model. A view has a birth (body, appear, task) and value changes (body, onChange). Everything in View runs on the main actor; anything else has to hop.

Order and isolation

What fires when, and which actor a view and its closures belong to.

  1. Trace one view through its first render and a value change: what is logged, in what order, and what about an equal write? 8.11.1 · L3
    The check logs a view with all four. On first render the order is body, then onAppear, then the task body starts: the body must produce the description before…
  2. Which actor do a view’s body, its task closures and its models run on in Swift 6, and what does the compiler enforce? 8.11.2 · L3
    SwiftUI calls body on the main thread and runs the closures you give .task, onChange and buttons on the main actor, so touching view state from them is fine…

More in SwiftUI