In SwiftUI, when an @Observable cart model changes, how does SwiftUI decide which views redraw, and what counts as a tracked read?
answer
- who read it, not who holds it
- body's execution scope
- action closures run later
- lazy rows depend on their own reads
basics
~20 sSwiftUI records which observable properties each view's body reads while it runs, and when one of them changes it updates only the views that read it. Reads outside body, such as in a Button action, subscribe nothing.
solid answer
~40 sWhile a view's `body` runs, every getter of an `@Observable` model reports the access, and SwiftUI ties those properties to that view. In a shop app, the tab badge reads `cart.itemCount`, so it updates when `items` changes; the checkout button reads `cart.isCheckingOut`, so editing `couponCode` leaves both alone. Holding the model is not a dependency: a screen that only passes `cart` to its children never redraws for a property change. Reads that happen later, in a `Button` action or an `onAppear` closure, are outside `body` and register nothing. Computed properties, nested observable objects and even a global model are tracked through the stored properties they read. That is the practical difference from `ObservableObject`, where any `@Published` change invalidates every view observing the object.
code
swift · 24 linesimport SwiftUI
struct CartTabBadge: View {
var cart: CartModel
var body: some View {
// Reads itemCount, so items: redraws when items changes.
Label("Cart", systemImage: "cart")
.badge(cart.itemCount)
}
}
struct CheckoutButton: View {
var cart: CartModel
var body: some View {
// Reads isCheckingOut in body: tracked.
Button("Check out") {
// Runs later, outside body: reads and writes here subscribe nothing.
cart.isCheckingOut = true
}
.disabled(cart.isCheckingOut)
}
}go deeper
Remember the one rule: a view updates when a property its body read changes, and only then.
Explain how getters report accesses during body, why a view that only passes the model on never redraws, and how List rows hold their own dependencies.
Use the rule to diagnose stale or over-eager screens: reads moved into action closures, a collection read too high in the tree, a reference swap mistaken for a property change.
Turn the rule into team guidance: read observable state as low in the tree as it is shown, and review data flow by asking which body reads what.
## The rule: a read in body subscribes With the **Observation** framework (iOS 17 and later), SwiftUI does not ask which views *hold* a model. It asks which views *read* which properties. Apple's documentation puts it in one line: Observation tracks changes to any observable property that appears in the execution scope of a view's `body`. Mechanically, the `@Observable` macro gives every tracked stored property a getter that reports an **access** to the model's registrar. Apple's documentation describes the result: a view forms a **dependency** on each observable property its `body` reads. When a tracked property changes, SwiftUI updates the views that depend on it; views that never read it are left alone. That is **per-property tracking**, and it is the difference from `ObservableObject`, whose `objectWillChange` invalidates every observing view whatever it read. ## Walking through the cart Take a `CartModel` with `items`, `couponCode` and `isCheckingOut`, and a computed `itemCount` that sums the quantities in `items`. | View | What its `body` reads | Redraws when | |---|---|---| | `CartTabBadge` | `cart.itemCount` (so `items`) | `items` changes | | `CouponField` | `cart.couponCode` | `couponCode` changes | | `CheckoutButton` | `cart.isCheckingOut` | `isCheckingOut` changes | | `CartScreen` | nothing; passes `cart` down | never, for a property change | Typing a coupon updates `CouponField` only. Adding an item updates the badge and whatever lists the items, but not the coupon field. ## What counts as a tracked read These reads create a dependency, as long as they happen while `body` runs: - a direct read such as `cart.couponCode`; - a **computed property** whose getter reads tracked stored properties; - a read through another observable object, such as `order.customer.name`, which tracks `name` on the customer; - a **collection** property such as `items`: inserting, removing, moving or replacing elements is a change to `items`; - a model reached through a **global or singleton**, which Apple's documentation shows tracking the same way. These do **not** create a dependency for the view: - reads inside closures SwiftUI calls later: a `Button` action, `onAppear`, `task`, a gesture handler; - reads in the view's initializer, which runs before `body`; - simply storing the model in a property and passing it to a child. ## Passing a model through layers Because holding is not reading, an `@Observable` model can travel through several intermediate views without any of them redrawing. Only the leaf view whose `body` reads the property is updated. One related rule is separate from Observation: a view that stores a reference **does** update if the reference itself changes, for example when a parent passes a different `CartModel` instance, because the stored reference is part of the view's value. ## Lazy containers and rows `List` and `ForEach` store their row closure and call it as rows are created. Apple's example shows the consequence: 1. The parent reads `cart.items` to know which rows exist, so it depends on the collection. 2. Each row's own `body` reads `item.name` or `item.quantity` for that row. 3. If the row type is an `@Observable` class, changing one row's property updates only that row, not the parent and not its siblings. If the rows are structs inside the `items` array, any edit to one element is a change to `items`, and everything that read `items` is updated. ## Mistakes to avoid - Expecting a view to update because it "has" the model, when its `body` never reads the property. - Reading a value inside a `Button` action and wondering why the label is stale; read it in `body` instead. - Wrapping a model in a wrapper just to "subscribe": with Observation, the read is the subscription. - Reading a large collection high in the tree when only a row needs it, which makes the high view redraw on every edit. How to measure redraws and split views to narrow them is covered with SwiftUI performance diagnosis; the point here is the rule itself.
- How does a List of observable cart rows avoid redrawing every row when one quantity changes?`List` stores its row closure and calls it lazily, so each row's `body` performs its own reads. If each row is an `@Observable` item object, changing one item's `quantity` updates only the row that read it. The parent depends on the `items` collection, so only inserts, removals, moves or replacements update it.
- A parent passes a different CartModel instance to a child that only stores it. Does the child update?Yes. That update is not Observation at work: the stored reference is part of the child's value, so a different instance makes the view value different and SwiftUI updates it. Per-property tracking only decides what happens when properties of the same instance change.
- Is a model read through a global variable tracked?Yes. Apple's documentation shows a view reading a global `@Observable` instance in `body` and forming a dependency on the property it read. Tracking depends on the read during `body`, not on how the view got hold of the object.
- Any change to an @Observable model redraws every view that holds it
- Reading a property in a Button action subscribes the view to it
- A view needs a property wrapper before it can observe an @Observable model
- Passing a model through an intermediate view makes that view redraw too
- Observation cannot track arrays or optionals of models