In a SwiftUI stocks dashboard backed by one ObservableObject, a price tick every second re-runs the header, the chart and all 500 rows; why, and how do you narrow it?
answer
- one signal for the whole object
- reads, not subscriptions
- per-symbol observable objects
- mutate in place, read where displayed
basics
~20 sAny @Published change fires objectWillChange, and every view observing that ObservableObject re-runs, read or not. Move to @Observable (iOS 17) with one object per symbol, mutate prices in place, and read each price only in its row.
solid answer
~40 sAn `ObservableObject` has one `objectWillChange` signal, so every view holding it through `@ObservedObject`, `@StateObject` or `@EnvironmentObject` re-runs on any `@Published` change, even one it never reads; often the root also reads `prices` and pushes new values down. With `@Observable`, iOS 17 and later, SwiftUI tracks per property: a view updates only when a property its body read changes. So I make each symbol an `@Observable` quote, apply ticks by setting `quote.price` in place rather than replacing the array, let each row read its own price, and keep the header and chart in their own views reading only their data. Then I confirm with `_printChanges()` and the SwiftUI instrument on a device.
code
swift · 35 linesimport SwiftUI
@Observable @MainActor final class Quote: Identifiable {
let id: String
var price: Double
init(id: String, price: Double) {
self.id = id
self.price = price
}
}
@Observable @MainActor final class Dashboard {
var quotes: [Quote] = []
// Mutate the existing object instead of replacing the array.
func apply(symbol: String, price: Double) {
quotes.first { $0.id == symbol }?.price = price
}
}
struct WatchlistView: View {
let dashboard: Dashboard
var body: some View {
List(dashboard.quotes) { quote in
QuoteRow(quote: quote)
}
}
}
struct QuoteRow: View {
let quote: Quote
var body: some View {
LabeledContent(quote.id, value: quote.price, format: .number.precision(.fractionLength(2)))
}
}go deeper
Recall that an ObservableObject change refreshes every view observing it, while @Observable refreshes only views that read the changed property.
Explain objectWillChange versus per-property tracking, and why a root that reads prices and passes them down re-runs too.
Diagnose with _printChanges, then restructure: per-symbol objects, in-place mutation, reads in the displaying view, and proof from the SwiftUI instrument.
Weigh migrating models to Observation against the deployment floor, and decide whether the feed rate itself should be coalesced.
## The symptom A stocks dashboard has three parts: a header with an index value, a chart of the day's index, and a `List` of 500 watched symbols. A feed updates prices every second. With one `ObservableObject` model, `Self._printChanges()` shows every one of those views re-running on every tick, and scrolling the list stutters. ## Why ObservableObject invalidates everything An `ObservableObject` announces changes through `objectWillChange`, which every `@Published` property triggers when it is set. A SwiftUI view that holds the object through `@StateObject`, `@ObservedObject` or `@EnvironmentObject` subscribes to that **one** signal for the whole object. Apple's migration guide states the consequence: "a view updates when any published property of an `ObservableObject` instance changes, even if the view doesn't read the property that changes." So in this dashboard: - the header, which shows only the index, re-runs when any symbol's price changes; - the chart re-runs and rebuilds its marks every second; - every row view that observes the shared model re-runs, whatever symbol it displays. A second, independent cause often sits on top: the root view **reads** `model.prices` and passes arrays or values down, so even views that do not observe the object get new values from a parent that re-ran. ## How Observation narrows it The `@Observable` macro (iOS 17) tracks **per property, per reader**. Apple's documentation: SwiftUI "updates a view only when an observable property changes and the view's body reads the property directly." The fix has three parts. 1. **Model the ticking data at the right grain.** Make each symbol its own `@Observable` object with a `price` property, held in the dashboard's `quotes` array. 2. **Mutate in place.** Apply a tick by setting `quote.price` on the existing object. Replacing the whole `quotes` array with freshly decoded values changes the `quotes` property itself, which invalidates every view that read it, the list included. 3. **Read where you display.** The list reads `quotes` (for the rows' identities); each row's own body reads its `quote.price`; the header reads only the index; the chart sits in its own view and reads only its series. After this, a tick on one symbol re-runs that symbol's row, if it has been created. Apple's documentation describes the `List` content closure being called lazily as rows are created, so a row that has not been created yet has no body to re-run. ## If you cannot adopt Observation yet On a deployment target below iOS 17, keep `ObservableObject` but break it up: - one small `ObservableObject` per row, so a row only observes its own changes; - pass plain values, not the object, to views that only display one field; - keep the header and chart off the per-symbol objects entirely. Moving an existing model from `ObservableObject` to `@Observable` (and `@StateObject` to `@State`) is covered with object ownership; the point here is the update scope each one produces. ## Fixes that look right but are not - **Wrapping every row in `.equatable()`.** It only gates new values from a parent; a row that holds the `ObservableObject` itself still re-runs on every `objectWillChange`. - **Switching `List` to a lazy stack.** `List` already creates rows lazily; the problem is how many created views depend on the object, not how many exist. - **Refreshing the screen from a timer.** Pulling prices once a second instead of pushing them changes nothing about which views each change touches. - **Giving the list a new `id` on each tick.** That recreates every row and resets its state; it hides the symptom by making the cost worse. ## Confirming the fix | Check | Before | After | |---|---|---| | `_printChanges()` in the header on a symbol tick | prints every second | silent | | `_printChanges()` in a row for another symbol | prints | silent | | SwiftUI template in Instruments, body evaluations per view type | header, chart and rows on each tick | only rows that display the changed symbol | Measure on a real device in a Release build. If a single row update is still slow, the cost is now inside the row's body, which is a different problem from the update scope. ## One more lever: tick rate A feed that delivers ten updates per second for the same symbol makes ten mutations, each a change SwiftUI may have to render. Coalescing ticks so that each symbol is updated at most once per display interval reduces work without changing the design. How fast the screen must update is a product and budget decision, covered with mobile performance budgets.
- The model is @Observable, but every tick still re-runs the whole list; what is the likely cause?The tick replaces `quotes` with a new array instead of mutating existing objects. Setting the array property is itself a change, so every view whose body read `quotes`, the list included, re-runs. Keep the objects and set their `price`.
- Why can a row view that never reads price still re-run under ObservableObject?Because it subscribes to the object's single `objectWillChange` signal through its property wrapper. Any `@Published` property firing invalidates every subscriber; SwiftUI does not know which properties the body read.
- What would you do on an iOS 16 deployment target?Keep `ObservableObject` but split it: one small object per row, plain values for views that show a single field, and nothing per-symbol observed by the header or chart. That restores most of the narrowing without the macro.
- a view re-runs only for the @Published properties its body reads
- replacing the whole array each tick is fine with @Observable
- List renders all 500 rows on every tick regardless of the model
- ObservableObject and @Observable produce the same update scope
- the fix is to wrap every row in .equatable()
Assumes iOS 17 or later for the @Observable macro; apps deploying to iOS 16 keep ObservableObject and split it into smaller objects instead.