22 of 22 · 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 |
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.
- @Observable tracks reads per property; ObservableObject invalidates the whole object on any change. Mark observable models @MainActor.
- A missing @Environment object crashes at runtime, not compile time. Inject at the root and in previews.
How I choose
| Local UI state | @State, private. |
|---|---|
| A child edits a parent's value | @Binding. |
| Shared app data | @Observable model, owned once, passed down. |
| Dependencies for a subtree | @Environment. |
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.
- @State is owned by the view, @Binding borrows a parent's value, @Environment hands dependencies down the tree, and @Observable models hold shared data with per-property tracking.
- My first question is who owns this state: one source of truth, everyone else borrows it.
- The common bugs are duplicated state that drifts and initializing @State from a parameter.
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 plain
let. - 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()so it lives as long as the view's identity. - Only the view that creates the instance should use
@State. - Views that receive it take a plain property or
@Bindable.
Two architecture proposals both handle model data fine. How do you compare them fairly?
- Compare them on the three problems every architecture must solve.
- How are components constructed and connected?
- How do view and model communicate?
- Where does non-model state, like selection and navigation, live?
- Model data is the easy part, so proposals rarely differ there.
- Ask each side to sketch the same feature and compare how they handle non-model state.