SwiftPumpkin

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

$valueWrites backOwns, passes downParent view@State ownsChild view@Binding borrowsModel@Observable, shared
calls or tellsreports backEvery piece of state has exactly one owner. Everyone else borrows it.
  • @State is owned by this view.
  • @Binding borrows from a parent. @Environment is handed down.
  • An @Observable model holds shared app data.
PartOwnsLooks like
@StateView-owned value or model lifetime@State private var draft
@BindingRead-write borrow from a parent@Binding var selection
@EnvironmentHanded down the tree@Environment(AppContainer.self)
@ObservableShared 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.
  • @Observable is a Swift macro that tracks stored var getters actually executed at runtime, so unvisited if or short-circuited branches aren't tracked until they run; unlike ObservableObject, unrelated views don't re-render. Mark internal mutable state @ObservationIgnored and 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
OwnerThe viewParentAncestorWhoever holds it
WritesThe viewChild, through parentVia the objectModel methods
LifetimeView identityParent'sAncestor'sOwner'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.

30-second answer

Explain State Management in 30 seconds.

SwiftAsked directly

With an @Observable model, how do you choose between @State, @Environment, @Bindable, or a plain property?

iOS codingAsked indirectly

A child Play button must toggle the parent's isPlaying, but passing a copy loses sync. What's the correct wiring?

SwiftAsked indirectly

After migrating to @Observable, a screen's model resets whenever its parent redraws. What's wrong?

System designAsked indirectly

Two architecture proposals both handle model data fine. How do you compare them fairly?

Read it in the app's source

Done with State Management? Mark it complete. Nailing its cards builds this pumpkin, row by row.