8.2 · The update loop, dependency tracking and the cost of body
SwiftUI redraws by re-evaluating body for the views whose inputs changed, not the whole screen. Understanding what invalidates a view is the whole performance story.
Mental model. A view is re-evaluated when something it read changes. Slow screens are almost always views that read too much, or body doing too much.
The update loop
What triggers <code>body</code> and what makes it expensive.
- How does SwiftUI decide to re-run body, and how do you keep updates cheap?
SwiftUI maintains a graph of attributes behind your views. When a view’s body reads a piece of state (a @State value, a binding, an environment value, or, with…
More in SwiftUI
- 8.1 · What a View is, and how SwiftUI knows which view is which
- 8.3 · State, Binding, Environment and Observation
- 8.4 · onAppear, task, onChange and scenes
- 8.5 · The layout negotiation, stacks, GeometryReader, safe area and custom Layout
- 8.6 · Lists, ForEach and NavigationStack
- 8.7 · Animations, PhaseAnimator, Canvas and TimelineView
- 8.8 · Preference keys and coordinate spaces
- 8.9 · Previews and the macros behind SwiftUI
- 8.10 · State, bindings, Observation and environment, checked by running them
- 8.11 · The order of events in a render, and what Swift 6 isolation means for views
- 8.12 · Sharing space, alignment guides, adaptive containers and grids, measured
- 8.13 · What a ViewBuilder produces, and saving and restoring a navigation path