M-Swift All articles
AI & Performance

Who's Actually Watching? Tracing SwiftUI Observation Dependencies After @Observable

M-Swift
Who's Actually Watching? Tracing SwiftUI Observation Dependencies After @Observable

Photo: The Next Web from Amsterdam, Netherlands, CC BY-SA 2.0, via Wikimedia Commons

Every team that shipped an @Observable migration in the past year or two has the same message buried in its Slack history: migration merged, feels snappier. Sometimes it is. Just as often, what changed is that the code reads as modern while the same view bodies keep firing, because nobody touched the dependency graph. @Observable is a tracking mechanism, not a performance switch — and the audit that actually matters starts after the merge.

What the migration really bought you

With ObservableObject, one @Published write sent objectWillChange down the pipe, and every view holding that object as @ObservedObject or @EnvironmentObject got invalidated — whether or not it rendered the property that changed. The standard remedy was to slice view models until the firehose turned into a garden hose.

@Observable, which showed up at WWDC 2023, tracks reads per property inside body. Write to cart.taxRate and the view that read taxRate invalidates; the view that only read cart.items doesn't. That's a genuine win and worth the migration. But the tracking is only as tight as your reads. If body touches the whole model, the whole model becomes a dependency.

Start with a baseline you trust

Two tools, both unglamorous, both sufficient.

Drop Self._printChanges() at the top of body:

var body: some View {
    #if DEBUG
    let _ = Self._printChanges()
    #endif
    VStack { /* ... */ }
}

The output tells you whether the re-evaluation was triggered by a changed property, a changed @State, or the parent (identity). Keep it behind #if DEBUG — the printing itself is not free in a list of two hundred rows.

The second tool is the SwiftUI instrument in Instruments, which records view body evaluations and, in current Xcode releases, shows cause-and-effect links between them. Record a real interaction on a physical iPhone, not the simulator. Sixty seconds of scrolling your slowest list tells you more than an afternoon of reading view code.

Write down three numbers before you change anything: body evaluations per second for the screen, the worst offender by count, and the worst offender by total time.

The four leaks that survive the migration

1. Body reads broader than the view renders

A detail screen needs a title and a status pill. It holds the whole Order and formats order.lineItems.map(\.name).joined() into a subtitle. The runtime doesn't care about intent — it cares about what body touched. That subtitle now makes the header depend on every line item, so any quantity edit repaints the header too.

Fix: compute the subtitle in a small value type, or hand a plain String to a child view. The parent never sees the array.

2. Array properties are all-or-nothing

Reading cart.items.count in a badge makes the badge depend on items. Bump one line's quantity and the badge invalidates. There's no per-element tracking on an Array, and there isn't going to be.

If the badge is a cheap Text, ignore it. If it sits above a lazy grid and drags the grid along with it, keep a derived itemCount: Int updated in the same mutation methods and let the badge read that. Two lines of bookkeeping buys you one dependency fewer.

3. Environment objects that are really app state

Injecting a single AppModel at the root is convenient and is the fastest known way to make every leaf depend on everything. @Environment(AppModel.self) in a view that reads .user.displayName is watching the user — fine — but the moment that same view also reads appModel.cart, you've rebuilt the firehose with extra steps. Inject the narrowest type that works, and inject it lower in the tree.

4. Bindings that pull in more than they need

Passing @Bindable var model into a TextField makes that child depend on the property being edited — correct. If the same child also reads model.items to show a footer, the text field now repaints on every cart change. Split the field and the footer into separate views and pass each one only what it renders.

Identity churn that looks like invalidation

A parent re-renders, rebuilds a child with a fresh .id(), or resets a child's @State because its position in the tree shifted. The result looks like over-observation but has nothing to do with it. _printChanges reports identity changes explicitly, and the cause-and-effect graph in Instruments usually makes the culprit obvious within a minute.

The loop we run per screen

  1. Record 30–60 seconds of the real interaction with the SwiftUI instrument open.
  2. Rank views by body count and by total body time.
  3. For the top three, ask one question: does this view need to read this, or does it need a value?
  4. Move the read down a level, or reduce it to a scalar.
  5. Re-record and put the before/after numbers in the PR description.

Ten to fifteen minutes per screen, assuming you don't get philosophical about architecture halfway through.

Know when to stop

Don't chase zero redraws. A Text re-evaluating is noise. A LazyVStack rebuilding forty rows because a badge changed a number is a bug worth a ticket. Aim for fewer dependencies where the work is expensive, not for maximum purity everywhere — otherwise you'll spend a sprint splitting views that were never the problem.

@Observable gave you a precise tool for tracking dependencies. The migration never told you which ones you actually have. That's still on you, one Instruments session at a time.

All Articles

Related Articles

The Dynamic Island Is Lying to You: A WidgetKit Refresh Audit That Finds the Real Lag

The Dynamic Island Is Lying to You: A WidgetKit Refresh Audit That Finds the Real Lag

Your App's First 400ms Are a Crime Scene: Triage Launch Time Before You Refactor

Where Did the Sprint Go? A One-Week Build-Time Audit for Swift Teams

Where Did the Sprint Go? A One-Week Build-Time Audit for Swift Teams