Skip to content

Lesson 4 of 9 · 26m

SwiftUI State Ownership That Survives Real Screens

Choose the correct owner for state, distinguish source of truth from derived values, and avoid the update loops that make SwiftUI feel unpredictable.

SwiftUI is easy for the first screen and confusing when a child edits a value, a parent reloads it, a sheet observes it, and a deep link resets it. That confusion is almost never a rendering problem. It is state ownership. If you cannot answer who owns a value, SwiftUI will answer in a way you dislike.

Every piece of state has one owner

State should live at the lowest level that owns its lifecycle, and no lower:

struct CheckoutView: View {
    @State private var showingAddressEditor = false
    @StateObject private var model: CheckoutModel
    init(model: CheckoutModel) {
        _model = StateObject(wrappedValue: model)
    }
    var body: some View {
        PaymentSection(selection: $model.paymentSelection, isValid: model.canSubmit)
    }
}

showingAddressEditor is local because nothing else cares whether the editor is visible. Payment selection belongs to the checkout model because validation, submission, and analytics care about it.

Source of truth beats copied truth

The classic bug is copying model data into view state and wondering why updates stop propagating:

@State private var displayName: String
init(profile: Profile) {
    _displayName = State(initialValue: profile.displayName)
}

Sometimes a copy is correct: edit forms isolate a draft until save. Then call it a draft and make the commit explicit. Accidental copies are bugs; deliberate copies are workflows.

Derived state should be computed, not stored

If a value can be calculated from authoritative inputs, storing it creates an invalidation problem:

var canSubmit: Bool {
    !model.cardNumber.isEmpty &&
    model.expirationIsValid &&
    !model.isSubmitting
}

Store inputs, compute the verdict. This applies to filtered lists, totals, validation status, formatted values, and button enablement.

Avoid update loops by reducing write amplification

A render that writes observed state can trigger another render, which writes again. The fix is not sprinkling DispatchQueue.main.async; it is separating events from state:

user action -> intent method -> model mutation -> published state -> view render

Do not mutate source-of-truth state inside body. Use task for lifecycle-bound async work, and make reload behavior idempotent because SwiftUI may call it more than once.

Field notes

  • @StateObject creates and owns the object; @ObservedObject observes one owned elsewhere. Mixing them up causes duplicate models and lost state.
  • environmentObject is convenient until dependencies become invisible. Prefer explicit injection for core services.
  • A binding is not a copy of truth; it is a read/write projection of state owned elsewhere.
  • If a sheet receives the wrong object instance, inspect where the object was created before blaming SwiftUI identity.

Was this lesson helpful?

Sign in to rate