A State default is not a StateObject
Apple’s Observation migration path is easy to skim as a find-and-replace: drop ObservableObject, drop @Published, keep the model in @State instead of @StateObject. The rename compiles. The lifetime does not follow. On Endeo and RuleCaddie we have watched “migrated” view models open stores, register notifications, and fire network work from init — then do it again every time SwiftUI rebuilt the view struct. A State default is ownership syntax. It is not the lazy, once-only construction StateObject gave you.
Two wrappers, two evaluation rules
StateObject’s wrapped value arrives through an autoclosure. SwiftUI can keep the existing instance when the view’s identity is stable and skip re-running the initializer expression. That is why a heavy ObservableObject parked in StateObject usually survived parent redraws without re-entering init.
State takes a value, not a deferred expression. On toolchains that still evaluate the default eagerly, constructing `@State private var model = NotebookStore()` allocates NotebookStore whenever SwiftUI instantiates the view struct — even when the previous instance is the one that will keep living. The throwaway allocations are invisible for a cheap struct flag. They are not invisible for a class that talks to disk, PencilKit, or the network in init.
Apple’s migration sample maps StateObject → State for a reason: Observation plugs into State and Environment, and views invalidate on the properties they actually read. That mapping is about observation wiring. It does not automatically mean “same construction semantics as StateObject on every OS and Xcode you still ship against.”
What breaks when init does real work
The failure mode is not a compile error. It is duplicate listeners, repeated UserDefaults reads, overlapping URLSessions, and stores that open more than once while a single screen is on glass. Testers report “it got chatty after the Observation refactor.” The diff looks like a macro swap. The bug is side effects in a default expression that now runs more often than the old autoclosure.
Navigation makes it worse. Popping back onto a screen does not guarantee the same view identity you had under StateObject. Conditional branches that recreate a child view recreate the default evaluation path. A model that “should only start once per session” quietly starts again.
We treat init on @Observable types the way we treat main-thread work under the pencil: if it is not cheap and idempotent, it does not belong there. Opening a notebook index, wiring NotificationCenter, or kicking a sync from init was always fragile. State just makes the fragility show up sooner.
Own the instance above the redraw
Put long-lived @Observable objects where the struct is not rebuilt for every frame of UI — typically the App or a Scene root — and pass them down, or install them in the environment the way Apple’s migration guide shows for shared models. View-local State is fine for screen-scoped, cheap state. It is the wrong shelf for a session store.
When a view must create the object, prefer an optional State and construct once in a task (or an explicit setup method you call once), not in the property default. Apple’s State documentation already warns against heavy default values for this reason. Observation did not invent that rule; the migration made more teams trip it.
Newer State macro behavior may initialize a class-backed State once on newer Xcode. Do not build the architecture around that luck. Keep init empty of I/O, keep ownership explicit, and the app stays correct on the toolchains your TestFlight matrix still includes.
Finer invalidation is a different win
Observation’s real product win is that a view body that never read `progress` does not re-run when `progress` changes. That is not the same problem as “how many times did NotebookStore’s init run.” Mixing the two in a postmortem produces the wrong fix — people sprinkle ObservationIgnored or split properties when the actual leak is construction lifetime.
Keep the checklist separate. Property tracking: which fields does this body read, and are ignored fields actually ignored. Lifetime: who creates the object, how many times can that path run, and what side effects still hide in init. Both matter after a migration. Only one is solved by the macro.
This sits next to the widget-writer and reload-budget notes the same way: SwiftUI surfaces invite looking like one object when the system gave you two clocks. Observation changed the invalidation clock. State vs StateObject is still the ownership clock.
What we check on an Observable migration
For every former StateObject, name the owner: App, Scene, parent, or view-local. If view-local, confirm the default expression allocates nothing that talks to disk, network, or notification centers. Prefer inject-from-above for anything that outlives a single screen.
Grep for work in init on @Observable types. Move it to an explicit start, a task, or a dependency you already own once. Confirm on a real device navigation loop — push, pop, rotate, background — that listeners and network clients do not multiply.
If those checks fail, the Observation refactor is incomplete even when every ObservableObject is gone. The milestone is one living instance per intended scope, with cheap construction and property-level invalidation — not a green compile after a wrapper rename.
Questions
- Should every @Observable live in @State?
- No. Shared session models belong at App or Scene scope (or in the environment). Use view-local State for screen-scoped state whose construction is cheap — or create it once in a task, not as a heavy default.
- Is @State with @Observable always wrong?
- No. It is the documented pairing. What is wrong is treating the State default like StateObject’s autoclosure and putting store opens, listeners, or network work in init.
- Does a newer State macro fix this?
- It may stop re-running class defaults on newer Xcode. Still keep init free of side effects and keep long-lived ownership above redrawing views so behavior does not depend on which toolchain built the binary.
Sources
- Apple — Migrating from ObservableObject to the Observable macro — Maps StateObject → State / Environment; property-level invalidation benefits.
- Jesse Squires — Observable is not a drop-in for ObservableObject — State vs StateObject initialization: value vs autoclosure; side effects in init.
- Apple — State — Avoid performance-intensive work in State default values; defer heavy setup.