skip to content

Three developers building a SwiftUI hotel-booking feature favour @Observable view models, shared environment models or a unidirectional store; how do you set the team's standard?

level: principalimportance: should knowfreq 40%

answer

  1. deployment target first
  2. where screen-only state lives
  3. who owns navigation
  4. testability and review cost
  5. one default, documented exceptions

basics

~20 s

Pick one default per kind of screen from the app's constraints: shared @Observable models for display and editing, @MainActor view models where a screen owns transient state, and a store only for flows whose effects need one writer. Write it down with exceptions.

solid answer

~40 s

I'd start from constraints, not taste. If the app still supports iOS 16, Observation is off the table there and the object wrappers decide. On iOS 17 and later, I'd set a default per kind of screen: shared `@Observable` models in the environment for screens that show and edit shared data, an `@MainActor @Observable` view model when a screen owns transient state such as loading, errors or a cancellable draft, and a unidirectional store only for a flow like checkout, where one writer, logged actions and ordered effects pay for the wiring. Navigation state gets one owner, a path in an observable router at the feature root. Then I'd prototype search, results and checkout under the rule, write a short decision record with when exceptions are allowed, and enforce it in templates and review.

go deeper

for a junior

Know the three shapes by name and what each one puts where: shared models, per-screen view models and a store with actions.

for a middle

Explain how each shape uses SwiftUI's tools: the environment, @Bindable, @MainActor view models and Binding(get:set:) for actions.

for a senior

Show the concrete costs in SwiftUI: stale copies, missing environment objects, store redraw storms and SwiftData's @Query staying in views.

for a principal

Turn the debate into rules with triggers, prototype them on real screens, give navigation one owner, and revisit when the deployment target or team changes.

## The decision A hotel-booking feature has three screens: **search**, **results** and **checkout**. One developer wants an `@MainActor @Observable` view model per screen, one wants shared `@Observable` models in the environment with no per-screen objects, and one wants a unidirectional store with reducers and effects. All three can ship. The lead's job is to pick a **standard** the team can apply without re-arguing it on every pull request, and to know what each option costs in SwiftUI specifically. ## Constraints that decide before taste does - **Deployment target.** Observation, `@Bindable` and `@Environment(Type.self)` need iOS 17. Screens that must run on iOS 16 use `ObservableObject` with `@StateObject`, which changes the cost of every option; migration is covered with the object wrappers. - **Persistence.** If the feature uses SwiftData, `@Query` is a dynamic property that SwiftUI updates as part of a view, which pulls fetching toward views and away from view models and stores. - **Testing.** Logic that must be unit-tested without views needs to live in an object or a pure function, not in `body`. - **Concurrency.** In Swift 6 language mode every view model or store that views read should be `@MainActor`, and every injected service `Sendable`; whichever shape you pick, the rule is the same and belongs in the standard. - **Previews.** Each shape needs a cheap way to render a screen in every state: sample models in the environment, view models built with fakes, or a store seeded with a state value. - **Team size and turnover.** A pattern only a few people understand becomes a review bottleneck. ## What each option costs in SwiftUI | Option | Strength | Cost | |---|---|---| | shared environment models | least code; per-property tracking for free | screen-only state has no obvious home; a missing injection throws at runtime | | view model per screen | clear home for loading, errors and drafts; easy unit tests | forwarding boilerplate; copies of shared data go stale | | unidirectional store | one writer, every change is an action, effects are ordered | wiring for each action; a single state struct re-runs every reader unless split | ## A workable standard Most teams land on a default **per kind of screen** rather than one pattern everywhere: 1. **Display and edit shared data** (search criteria, the booking session): read the shared `@Observable` model from the environment; bind with a local `@Bindable`. 2. **Screens with their own transient state** (results with loading and errors): an `@MainActor @Observable` view model, services injected through `init`, state `private(set)`, logic in intent methods. 3. **Flows where ordering and auditability matter** (checkout, payment, refunds): a store with actions and effects, with state split into separate stored properties so views track only what they read. Write each rule with its **trigger**, for example "a screen gets a view model when it owns state no other screen reads", so a reviewer can apply it. ## Where navigation state lives Navigation is where the three options collide. Give it one owner: a typed array or `NavigationPath` stored in an observable router at the feature root and bound to `NavigationStack(path:)`. View models and stores ask the router to push a route instead of holding paths of their own, deep links mutate the router, and the destination views are built in `navigationDestination(for:destination:)`. Path mechanics are covered with stack-based routing. ## Rolling it out - **Prototype** search, results and checkout under the proposed rules before writing them down; measure lines of wiring and how easily a new developer follows them. - Record a short **decision record**: the default, the triggers for each exception and the reasons. - Ship **templates** for a screen with and without a view model so the default is the easy path. - Add review checks: `@MainActor` on every view model or store, no business logic in `body`, no copies of shared data, one navigation owner. - Revisit when a constraint changes: dropping iOS 16, adopting SwiftData, or the team doubling. ## What a lead should not do - Mandate a store everywhere because one flow needs it; display-only screens pay its wiring for nothing. - Allow all three without rules; each screen then becomes a debate and onboarding costs grow. - Treat the choice as permanent. A screen that gains transient state can grow a view model later, because the shared models it read stay the same.

  • How would the standard change if the app must still support iOS 16?
    Observation, `@Bindable` and `@Environment(Type.self)` need iOS 17, so iOS 16 screens use `ObservableObject` with `@StateObject` and `@EnvironmentObject`, which invalidate on any published change. I'd keep the same shapes, shared models, view models and an occasional store, but write them so each type can switch to `@Observable` when the deployment target moves, and plan that migration explicitly.
  • Where should a deep link into checkout land under your standard?
    In the feature's navigation owner. The app's `onOpenURL` handler parses the link and asks the observable router to set its path to the checkout route; the stack bound to that path shows it. View models and stores never parse URLs or hold their own paths, so there is one place to test and one place that can be wrong.

  • One pattern must be used for every screen, whatever the screen does.
  • The deployment target doesn't affect which state pattern a team can use.
  • Every view model or store should own its own navigation path.
  • Choosing an architecture is a matter of developer preference only.