skip to content

In SwiftUI, how does an ObservableObject class with @Published properties cause views to update, and what does objectWillChange signal?

level: juniorimportance: should knowfreq 58%

answer

  1. a protocol from Combine
  2. will change, not did change
  3. the publisher carries no value
  4. every observer, every published change

basics

~10 s

Setting a @Published property makes the object's objectWillChange publisher emit before the value changes; SwiftUI invalidates every view holding the object in @StateObject, @ObservedObject or @EnvironmentObject and re-runs their bodies, whichever property they read.

solid answer

~30 s

`ObservableObject` is a Combine protocol whose single requirement is `objectWillChange`, synthesized as an `ObservableObjectPublisher` with a `Void` output. Each `@Published` property makes that publisher emit just before a new value is stored. SwiftUI subscribes only when the object is held in `@StateObject`, `@ObservedObject` or `@EnvironmentObject`; on the signal it invalidates those views and re-reads the model when `body` runs again. Because the signal names no property, *every* observing view redraws on *any* published change. Non-published properties do not trigger updates unless you call `objectWillChange.send()` before mutating them.

code

swift · 25 lines
swift
import SwiftUI

@MainActor
final class FeedModel: ObservableObject {
    @Published var headlines: [String] = []
    @Published var isLoading = false

    // Not published, but still meant to redraw: signal by hand.
    var readIDs: Set<String> = [] {
        willSet { objectWillChange.send() }
    }

    // Not published and never signalled: changing it redraws nothing.
    var cacheHits = 0
}

struct FeedHeader: View {
    @ObservedObject var model: FeedModel

    var body: some View {
        // Redraws when headlines, isLoading or readIDs changes,
        // although only headlines is read here.
        Text("\(model.headlines.count) stories")
    }
}

go deeper

for a junior

Recall the pieces: a class conforms to ObservableObject, marks UI-relevant properties @Published, and a view holds it with an object wrapper so changes redraw it.

for a middle

Explain that objectWillChange is a Void signal sent before the change, synthesized for you, and that SwiftUI re-reads the model when body runs.

for a senior

Show you know the cost: whole-object invalidation redraws every observer on any published change, and you shape models or views to limit it.

for a principal

Frame ObservableObject as the pre-iOS 17 baseline and weigh its coarse invalidation against the migration effort to per-property Observation.

## What ObservableObject is `ObservableObject` is a protocol from the **Combine** framework, available since iOS 13, that SwiftUI uses as its original way to watch a reference-type model. The protocol is class-only (it refines `AnyObject`) and has one requirement: a publisher named `objectWillChange`. When a class declares the conformance and does not supply its own publisher, the compiler synthesizes `objectWillChange` as an `ObservableObjectPublisher`, whose output type is `Void`. That detail matters: the publisher carries **no value**. It is a signal that says "this object is about to change", nothing more. **`@Published`** is the Combine property wrapper that connects a stored property to that signal. When code assigns a new value to a published property, the owning object's `objectWillChange` emits before the new value is stored. The wrapper only works on properties of a class; on a struct property the compiler rejects it with the message that `@Published` is only available on properties of classes. ## How SwiftUI turns the signal into a redraw A view does not observe a model just because it holds a reference to one. SwiftUI subscribes to `objectWillChange` only when the object sits in one of the object wrappers: - **`@StateObject`**: the view creates and owns the object. - **`@ObservedObject`**: the view receives an object someone else owns. - **`@EnvironmentObject`**: the view reads an object an ancestor put into the environment. When the signal fires, SwiftUI marks every view that observes the object as invalid and runs its `body` again on a later update. Because the signal arrives *before* the change, SwiftUI does not read anything at that moment; the new values are read when `body` runs. A property that is not marked `@Published` never fires the signal, so changing it alone does not redraw anything; Apple's documentation recommends publishing only properties that can change and that matter to the UI, because each published property has a cost. Two near-misses do **not** observe published changes: 1. A plain `let model = FeedModel()` stored in a view: SwiftUI sees an ordinary reference and never subscribes. 2. A `@State` property holding an `ObservableObject`: the documentation for `State` says the view then updates only when the reference itself is replaced, not when a published property changes. ## Granularity: the whole object, not the property The signal names no property, so SwiftUI cannot know *which* property changed. Every view observing the object is invalidated by **any** published change, even a view whose `body` reads none of the changed data. Apple's migration guide states it directly: with `ObservableObject`, a view updates when any published property changes, even if the view does not read it. On a large view model shared by many screens, that is where extra `body` runs come from. The newer Observation framework (iOS 17 and later) tracks the properties each `body` actually reads instead, which is one of the main reasons teams migrate. | Situation | Does the observing view redraw? | |---|---| | A `@Published` property it reads changes | Yes | | A `@Published` property it never reads changes | Yes, the whole object signalled | | A non-published stored property changes | No | | `objectWillChange.send()` is called manually | Yes | ## Signalling by hand Sometimes state that is not a `@Published` property must still redraw views, for example a computed value backed by a private store or a property you deliberately keep unpublished. Call `objectWillChange.send()` **before** the mutation, which keeps the same "will change" contract that `@Published` follows. A `willSet` observer on the property is a tidy place for it. ## Practical rules - Mutate published properties on the main actor; most teams mark view models `@MainActor` so the compiler enforces it. - Keep the published surface small: publish what the screen renders, not every cache or counter. - Subscribing to `$property` publishers in your own code is Combine work, covered with Combine itself; for SwiftUI, the object wrappers do the subscribing. - New code that can require iOS 17 or later usually uses `@Observable` instead; `ObservableObject` remains common in apps that still deploy to iOS 16. ## Mistakes reviewers catch - A struct marked `@Published`: it does not compile, and the fix is `@State` for view-owned values, not a wrapper change. - A model that conforms to `ObservableObject` but publishes nothing: views holding it never refresh, because nothing ever fires the signal. - A manual `objectWillChange.send()` placed after the mutation: it no longer matches the will-change contract that `@Published` follows; send first, then mutate. - One giant view model observed by every screen: correct, but every keystroke in one field invalidates all of them.

  • Why does it matter that objectWillChange fires before the change rather than after?
    The publisher's output is `Void`, so it never delivers the new value. SwiftUI uses the signal only to mark observing views invalid and reads the model later, when `body` runs. A manual `objectWillChange.send()` should therefore come before the mutation too, so it follows the same contract as `@Published`.
  • A screen with five subviews all observe one large view model. What does that cost?
    Any published change invalidates all five, even subviews that read nothing that changed. Splitting the model, passing only the values a subview needs, or moving to `@Observable` (iOS 17 and later), which tracks the properties each `body` reads, reduces those extra `body` runs.

  • Believes @Published works on struct properties too
  • Thinks a view only redraws when a property its body reads changes
  • Claims objectWillChange delivers the new value to subscribers
  • Stores an ObservableObject in a plain let and expects updates
  • Says every stored property of an ObservableObject is observed automatically