skip to content

Your SwiftUI news app still supports iOS 16 and uses ObservableObject view models everywhere; how would you decide on and sequence a migration to @Observable?

level: principalimportance: should knowfreq 42%

answer

  1. the deployment floor comes first
  2. one model type at a time
  3. four wrappers, four replacements
  4. the toolchain changes State's initializer

basics

~20 s

Observation requires iOS 17, so raising the deployment target is the first decision. Then migrate one model type at a time, updating every wrapper site, and plan for State initialization, lost publishers and changed update behaviour.

solid answer

~40 s

`@Observable` and its SwiftUI companions are available from iOS 17, so the gate is a product call on dropping iOS 16. The payoff is per-property tracking, tracked optionals and collections, and simpler wrappers. Migrate incrementally by type, since both systems can coexist: convert a model with few consumers, then in the same change turn `@StateObject` into `@State`, `@ObservedObject` into a plain property or `@Bindable`, and `@EnvironmentObject` into `@Environment(Type.self)` with `environment(_:)`. Plan for three traps: built with Xcode 26 or earlier, `@State` instantiates its default value on every view initialization (Xcode 27's `State()` macro does it once), `$property` publishers disappear, and views relying on whole-object invalidation may stop refreshing.

code

swift · 32 lines
swift
import SwiftUI

// Before (iOS 16): ObservableObject + object wrappers
// final class FeedModel: ObservableObject { @Published var stories: [String] = [] }
// @StateObject private var model = FeedModel()
// @EnvironmentObject var session: Session

// After (iOS 17 and later)
@Observable
@MainActor
final class FeedModel {
    var stories: [String] = []
    var query = ""
    @ObservationIgnored var lastFetch: Date?
}

struct FeedScreen: View {
    @State private var model = FeedModel()   // was @StateObject

    var body: some View {
        FeedSearch(model: model)
        List(model.stories, id: \.self) { Text($0) }
    }
}

struct FeedSearch: View {
    @Bindable var model: FeedModel            // was @ObservedObject

    var body: some View {
        TextField("Search", text: $model.query)
    }
}

go deeper

for a junior

Know that @Observable needs iOS 17 and that @StateObject becomes @State and @EnvironmentObject becomes @Environment after migrating.

for a middle

Map each object wrapper to its Observation replacement and explain why @Bindable appears where a view needs bindings into the model.

for a senior

Anticipate the behavioural changes: State initialization differences by toolchain, lost Combine publishers, and views that relied on whole-object invalidation.

for a principal

Own the go or no-go: weigh the iOS 16 user share, where per-property tracking pays off, the sequencing and the rule that stops new ObservableObject types.

## The decision in front of the lead A news app still deploys to iOS 16, and every feature uses `ObservableObject` view models with `@StateObject`, `@ObservedObject` and `@EnvironmentObject`. The question is not how one wrapper maps to another; it is **whether**, **when** and **in what order** to move the codebase to Observation's `@Observable`, and what can break on the way. There is no single right plan; there is a set of constraints and a sequencing that respects them. ## Constraint 1: the deployment target The `@Observable` macro, the `Observable` protocol, `@Bindable`, `environment(_:)` with an object and `@Environment(Type.self)` are all declared available from **iOS 17** in the SDK. A type cannot be `@Observable` on iOS 16, and keeping two versions of every model behind availability checks doubles the code to maintain. In practice the first step is a product decision: raise the minimum deployment target to iOS 17, using the share of active users still on iOS 16 as the input. Until then, the right move is usually to keep `ObservableObject` and avoid writing new code that would make the migration harder, such as more Combine subscriptions on `$property` publishers. ## What the migration buys Apple's migration guide lists the benefits, and they are the business case: - **Per-property tracking**: a view updates only when a property its `body` reads changes, instead of on every published change of the object. - **Optionals and collections of objects** are tracked, which `ObservableObject` cannot do. - **Simpler data flow primitives**: `@State` and `@Environment` replace `@StateObject` and `@EnvironmentObject`; plain properties replace most `@ObservedObject` uses. ## Sequencing that works Migration is **incremental by type**: an app can mix both observation systems, so one model can move at a time. 1. Raise the deployment target and ship that alone. 2. Pick a model with few consumers and convert it: replace the conformance with `@Observable`, remove `@Published`, mark untracked properties with `@ObservationIgnored`. 3. In the same change, update every site that holds that type: owning `@StateObject` becomes `@State`; `@ObservedObject` becomes a plain property, or `@Bindable` where the view needs bindings; `@EnvironmentObject` becomes `@Environment(Type.self)`, and `environmentObject(_:)` becomes `environment(_:)`. The object wrappers require `ObservableObject`, so leftover sites stop compiling, which is a useful checklist. 4. Move outward to shared, app-wide models last, once the team trusts the new behaviour. 5. Add a lint or review rule that rejects new `ObservableObject` types. ## The traps to plan for | Trap | Why it bites | Mitigation | |---|---|---| | `@State` initial value | Built with Xcode 26 or earlier, the `State` property wrapper instantiates its default value every time SwiftUI instantiates the view, unlike `@StateObject`'s once-only autoclosure | Keep model initializers cheap and start work from `task`; with Xcode 27 the `State()` macro instantiates it the first time only | | Fewer updates | Only properties read in `body` are tracked; a view that relied on whole-object invalidation may stop refreshing | Test each screen; read what you display in `body` | | Lost publishers | `$property` and `objectWillChange` subscriptions disappear with `@Published` | Replace with `withObservationTracking` (iOS 17) or the `Observations` sequence (iOS 26) | | Missing environment object | `@Environment(Type.self)` still fails when nothing is injected | Use the optional form where absence is legal | The first trap is the toolchain-sensitive one: the same source behaves differently depending on whether it is built with Xcode 27, so a team that builds release candidates with an older Xcode must budget for it. ## How to measure and decide - Before and after each step, compare `body` runs on the busiest screens with the SwiftUI performance tooling. - Track crash reports for environment lookups during the rollout. - Stop when the remaining `ObservableObject` types are stable, rarely touched code; a migration with no user-visible gain can wait for the next rewrite of that feature. ## Communicating the plan - Write down the deployment-target decision with the user-share data behind it, so the team does not relitigate it per pull request. - Publish the wrapper mapping and the three traps as a short migration guide in the repository, next to an example pull request. - Agree on a definition of done per feature: no `ObservableObject` types, no object wrappers, previews updated, and the screen's update behaviour checked by hand. The principal-level answer weighs these against the roadmap: the migration is cheap per type but touches many files, and its payoff concentrates in large, frequently changing models shared by many views.

  • Why migrate a model and all its wrapper sites in one change instead of the model first?
    The object wrappers require `ObservableObject`, so once the conformance is gone every `@StateObject`, `@ObservedObject` and `@EnvironmentObject` holding that type stops compiling. Doing the type and its sites together keeps each change buildable and reviewable, and the compiler errors double as the checklist.
  • A migrated screen stops refreshing a label. What do you check first?
    Whether the label's data is read in `body`. Observation updates a view only when a property its `body` reads changes; data reached through a property marked `@ObservationIgnored`, a non-observable store, or a read outside `body` no longer triggers updates the way whole-object invalidation did.
  • When would you advise not to migrate yet?
    When a meaningful share of users is still on iOS 16, when the remaining models are small and rarely change, or when a feature rewrite is already planned. Coarse invalidation costs little on small models, and the migration's payoff is concentrated in large, shared, frequently changing ones.

  • Plans to adopt @Observable while still deploying to iOS 16
  • Rewrites every model in one release with no incremental path
  • Swaps @StateObject for @State without checking the model's initializer cost
  • Keeps @ObservedObject on @Observable types after migrating
  • Assumes views update exactly as before after migration
  • Forgets code subscribed to $property publishers or objectWillChange

Assumes Xcode 27 with the iOS 27 SDK, where @State is the State() macro and creates a class-typed default value only on first instantiation; Observation itself needs iOS 17.