In SwiftUI, why does extracting part of a body into its own View struct narrow redraws when a computed property returning some View does not?
answer
- dependencies are recorded per body
- computed property runs in parent's body
- child reads, parent passes the object
- split along what ticks
basics
~20 sSwiftUI tracks dependencies per view body. A computed property runs inside the parent's body, so its reads make the parent re-run; a separate View struct has its own body, so only it re-runs when what it reads changes.
solid answer
~40 sSwiftUI records what each `body` reads and re-runs that body when one of those values changes. A `private var priceLabel: some View` or a `@ViewBuilder` function is just code executed inside the parent's body, so reading `quote.price` there makes the whole parent depend on `price`. Moving it into `struct PriceLabel: View` that receives the `@Observable` quote and reads `price` in its own body means a tick re-runs only `PriceLabel`; the parent creates a cheap struct and never reads the value. It doesn't help if the parent still reads the property to pass it down, if the child gets a new value every parent run, or if the child observes an `ObservableObject`, which re-runs on any published change.
code
swift · 33 linesimport SwiftUI
@Observable final class Quote {
var symbol = "AAPL"
var price = 187.5
}
// The computed property runs inside InlineHeader's body,
// so InlineHeader re-runs on every price tick.
struct InlineHeader: View {
let quote: Quote
var body: some View {
VStack { Text(quote.symbol); priceLabel }
}
private var priceLabel: some View {
Text(quote.price, format: .number)
}
}
// SplitHeader never reads price; only PriceLabel re-runs on a tick.
struct SplitHeader: View {
let quote: Quote
var body: some View {
VStack { Text(quote.symbol); PriceLabel(quote: quote) }
}
}
struct PriceLabel: View {
let quote: Quote
var body: some View {
Text(quote.price, format: .number)
}
}go deeper
Recall that a separate View struct has its own body and its own updates, unlike a computed property inside the parent.
Explain that reads are recorded in the body that executes them, and why passing the object rather than the value keeps the parent out of the dependency.
Name the cases where a split saves nothing, such as a parent that still reads the value or an ObservableObject child, and verify with _printChanges.
Set the team habit of splitting along update frequency, weighing the gain against a codebase fragmented into views that do not need to exist.
## Dependencies belong to a body SwiftUI decides what to update **one view body at a time**. While a view's `body` runs, SwiftUI records what it read: observable properties, the view's own state, environment values. Apple's documentation puts the Observation rule plainly: "Observation tracks changes to any observable property that appears in the execution scope of a view's body property." When one of those recorded things changes, **that** body runs again, and so do any children whose new values SwiftUI treats as changed. The unit of tracking is therefore the `View` struct that owns a `body`, not the lines of code you group visually. ## Why a computed property does not narrow anything A common way to tidy a long body is a private computed property: ```swift private var priceLabel: some View { Text(quote.price, format: .number) } ``` It reads well, but it is just code that runs **inside the parent's body**. The read of `quote.price` happens in the parent's execution scope, so the parent depends on `price`. On every price tick the whole parent body runs again, including the symbol, the chart and everything else it builds. The same is true of a `@ViewBuilder` function and of a local closure called from `body`. ## Why a separate View struct does Move the same code into its own type: ```swift struct PriceLabel: View { let quote: Quote var body: some View { Text(quote.price, format: .number) } } ``` Now `price` is read in `PriceLabel`'s body. The parent only creates `PriceLabel(quote: quote)`, which is a cheap struct holding a reference, and never reads `price`. Apple's documentation describes this pattern: an observable object can "pass through multiple layers of a view hierarchy without each intermediate view forming a dependency." In a test run of exactly this pair of headers, a price tick re-ran `InlineHeader` (computed property) but, on the split side, only `PriceLabel`; `SplitHeader` did not run. Splitting also helps when the **parent** has to re-run for another reason. A child whose inputs did not change is not re-evaluated: in the same experiment, a static `Text`-wrapping child with an unchanged string did not re-run when its parent did. ## What happens when the parent re-runs anyway Sometimes the parent must re-run, for example because it shows the symbol and the symbol changed. SwiftUI then evaluates the parent's body, which creates a fresh `PriceLabel` value, and compares it with the previous one. If the child's inputs are unchanged (the same `Quote` reference here), SwiftUI does not run the child's body. With `Self._printChanges()` in both, you see the parent's line and no line for the child; if the child's inputs did change, its line reads `@self changed.`, which is the signal that the parent is passing it something new. ## When splitting does not help | Situation | Why the split changes nothing | |---|---| | The parent still reads the volatile property, e.g. `PriceLabel(price: quote.price)` | the read is in the parent's body, so the parent re-runs and passes a new value | | The child's input changes on every parent run, such as `Date()` or a fresh array | the child gets a new value each time | | The child holds an `ObservableObject` with `@ObservedObject` | any `@Published` change re-runs it, whether or not it reads that property | | The extracted piece is tiny and changes together with the parent anyway | no update is saved; only readability changes | ## How to split well 1. **Split along update frequency.** Things that tick (a price, a timer, a progress value) go into the smallest view that displays them. 2. **Pass the object, read in the child.** Hand down the `@Observable` reference (or a `Binding`) and let the child read the property it shows. 3. **Keep stable inputs stable.** Do not create new objects or timestamps in the parent's body just to pass them down. 4. **Verify.** Put `let _ = Self._printChanges()` in parent and child, trigger the change, and check that only the child prints. Splitting has almost no cost of its own: views are value types SwiftUI is designed to create often. The cost of over-splitting is readability, so split where the update boundary is, not at every `HStack`. Lists already work this way: Apple notes that a `List`'s content closure is called lazily as rows are created, so a row that reads `book.title` depends on it, and the view that owns the list does not.
- Does passing quote.price instead of quote to the child undo the gain?Yes. Writing `PriceLabel(price: quote.price)` reads `price` in the parent's body, so the parent depends on it and re-runs on each tick before passing the new value down. Pass the observable object, or a binding, and let the child do the read.
- Is there a cost to creating many small child views?Very little at runtime: views are value types that SwiftUI expects to create often, and a child with unchanged inputs is not re-evaluated. The real cost is readability, so split at update boundaries, where something ticks, rather than at every stack.
A newsroom where each reporter follows only their own beat: a sports score changing interrupts the sports desk, not the editor who merely hands out assignments. Make the editor read the score aloud before handing it over, and the editor is interrupted too.
- a computed property returning some View is tracked as its own view
- @ViewBuilder functions get their own dependency tracking
- splitting views is purely cosmetic and never changes redraws
- passing the read value down is the same as passing the object
- every child re-runs whenever its parent's body runs