20 of 20 · SwiftUI architecture
State Management
SwiftUI state management is about ownership: every piece of state has exactly one owner, and every other view borrows it. The property wrapper you choose declares who owns what.
How it's wired
- @State is owned by this view.
- @Binding borrows from a parent. @Environment is handed down.
- An @Observable model holds shared app data.
| Part | Owns | Looks like |
|---|---|---|
| @State | View-owned value or model lifetime | @State private var draft |
| @Binding | Read-write borrow from a parent | @Binding var selection |
| @Environment | Handed down the tree | @Environment(AppContainer.self) |
| @Observable | Shared model, per-property tracking | @MainActor @Observable class |
Swift example: one owner, zero eager-init side effects
Own an @Observable model once with @State, keep its init cheap, and pass plain references or @Bindable to children.
Owner view vs borrowing child
Start async work in .task (tied to view lifetime), never inside Model.init.
@MainActor @Observable
final class NoteEditorModel {
var message = ""
private(set) var isLoaded = false
// Cheap, side-effect-free init: safe even when SwiftUI re-evaluates @State's default.
init() {}
func load() async {
guard !isLoaded else { return }
isLoaded = true
}
}
struct NoteEditorScreen: View {
@State private var model = NoteEditorModel()
var body: some View {
NoteEditorForm(model: model)
.task { await model.load() }
}
}
struct NoteEditorForm: View {
@Bindable var model: NoteEditorModel // borrows to produce $model.message
var body: some View {
TextField("Thank you", text: $model.message)
}
}If a child only reads properties and needs no bindings, take a plain let model: NoteEditorModel instead of @Bindable.
Mistakes that ship
- Initializing @State from a parameter uses only the first value. Later changes from the parent are ignored.
- Duplicated sources of truth drift. Derive values instead of copying them into new state.
@Observableis a Swift macro that tracks storedvargetters actually executed at runtime, so unvisitedifor short-circuited branches aren't tracked until they run; unlikeObservableObject, unrelated views don't re-render. Mark internal mutable state@ObservationIgnoredand models@MainActor.- Unlike @StateObject (@autoclosure), @State private var model = Model() evaluates Model() on every parent redraw and discards extra instances. Keep Model.init cheap and side-effect-free.
- A missing @Environment object crashes at runtime, not compile time. Inject at the root and in previews.
How I choose
| Local UI state (toggles, sheet flags) | @State, private. |
|---|---|
| A child edits a parent's value | @Binding. |
| Shared app or screen data | @Observable owned once with @State (cheap init); pass plain let or @Bindable to children. |
| Dependencies for a subtree | @Environment, injected at the root and in previews. |
Compared
Who owns the state
| @State | @Binding | @Environment | @Observable | |
|---|---|---|---|---|
| Owner | The view | Parent | Ancestor | Whoever holds it |
| Writes | The view | Child, through parent | Via the object | Model methods |
| Lifetime | View identity | Parent's | Ancestor's | Owner's @State |
Expert notes
- Ask "who owns this?" before picking a wrapper. One owner, everyone else borrows.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain State Management in 30 seconds.
- SwiftUI state is about ownership and lifetime.
@Statestorage belongs to the view's identity, which is why it survivesbodyre-running.@Bindingborrows a parent's value,@Environmentpasses dependencies down the tree, and an@Observablemodel tracks which properties each view reads.- My default is one
@Observableview model per screen, owned with@State, with navigation in a router. - The common bugs are duplicated state that drifts, and
@Stateseeded from a parameter, which ignores later updates.
With an @Observable model, how do you choose between @State, @Environment, @Bindable, or a plain property?
- Ask three questions.
- Does this view own the model? Use
@State. - Does it come from far up the tree? Use
@Environment. - Does the view only need bindings to edit it? Use
@Bindable. Otherwise use a plainlet. - With Observation, views re-render only when properties they actually read in
bodychange, so the wrapper only decides ownership and lifetime.
A child Play button must toggle the parent's isPlaying, but passing a copy loses sync. What's the correct wiring?
- The parent owns
@State private var isPlaying = false. - The child declares
@Binding var isPlaying: Boolwith no default, and the parent passes$isPlaying. - Now there's one source of truth, and the child reads and writes it without owning it.
- Reading it in
bodycreates the dependency, so SwiftUI updates both views with no manual syncing.
After migrating to @Observable, a screen's model resets whenever its parent redraws. What's wrong?
- The view holds its
@Observablemodel as a plain property, so every parent redraw creates a fresh instance and resets it. - Wrap it in
@State private var model = Model()in the owning view only, and pass a plain property or@Bindableto receivers. - Keep
Model.initcheap and side-effect-free: unlike@StateObject,@Stateevaluates the initializer on every parent redraw and discards extra instances.
Two architecture proposals both handle model data fine. How do you compare them fairly?
- I'd compare them on four boundaries: where business logic lives, who owns non-model state like selection, loading and navigation, how dependencies are injected, and how behavior is tested without the UI.
- Then both sides build the same representative feature, one with async loading and navigation, and I compare the diffs and the tests.
- I'd pick whichever makes the next change easier to get right, even if it's more code today.
- Without contrary evidence, I'd expect MVVM with a router to win, because state ownership and navigation each get one home.
Read it in the app's source
Done with State Management? Mark it complete. Nailing its cards builds this pumpkin, row by row.