Understanding when SwiftUI re-renders an @Observable

This seems to be correct:

@Observable class Box<T: Equatable> {
  var value: T? {
    didSet {
      self.count += 1
    }
  }
  
  @ObservationIgnored var count = 0
}

let box = Box<Car>()
let c1 = Car(name: "1234")
box.value = c1
let c2 = Car(name: "1234")
box.value = c2

precondition(box.count == 2)

Passes on 6.0 and breaks on 6.2.

@Philippe_Hausler it looks like we are targeting 2025-04-02 for a release/6.2 branch. Would you have any interest in collaborating on something like a "retroactive" evolution pitch for the shouldNotifyObservers changes? Would it be possible to see if the community had any more ideas that might lead to some potentially different implementations for shouldNotifyObservers before this goes to production later this year?

Sure, if you want to put together a pitch document about it id be happy to help out give context and information of the impact and design decisions around it. If anything it might be best for just bookkeeping of what has changed.

Feel free to hit me up as a review on a pre-pull-request even for getting that together.

*as a side note; now that the Observed pitch is live - that is one of the major reasons to have that modification btw. While testing that before pitching we basically found that most folks needed to add in the uniqueness checks and a lot of other projects were already doing that for perf reasons for SwiftUI anyhow.

3 Likes

For now I was thinking of something a little more lightweight. Just a post in the Evolution page for some more traffic on the discussion that started here:

  1. Quick introduction of the discussion.
  2. Link to the original diff that landed.
  3. Link to this discussion thread.
  4. What do we want to do before release/6.1 is cut?
    a. Do nothing. We ship the feature as it is written on main.
    b. Roll it back. Remove the shouldNotifyObservers code and spend more time talking about options.
    c. Write more code. We change the functionality of shouldNotifyObservers before the branch cut.

Either way I strongly recommend for someone to write the detailed documentation on this feature before it goes live to prod… but documentation diffs can patch after the branch cut without much trouble. But product engineers should have some definitive source to learn from when they compile their code with the 6.2 toolchain and certain behaviors of Observable objects has changed.