Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"a new Observations struct that is created with a closure, and provides an AsyncSequence that emits new values whenever any any @Observable data changes"

Is this another asinine onChange()-style mechanism that actually means WILL change? In other words, it tells you BEFORE the value is set on the object, so you can't do jack squat with it much of the time.

That's the M.O. of onChange now, which is utterly brain-dead. Gee, I've been told that a value changed in this object, so let's recalculate some stuff. WHOOPS, nope, none of the other objects (or hell, even the affected object) can take action yet because they can't interrogate the object for its new contents.

Truly incredible that they not only defaulted, but LIMITED change-detection to WILL-change, the least useful of the two choices.



> Truly incredible that they not only defaulted, but LIMITED change-detection to WILL-change

That's not strictly true. You get the new value inside the closure. This is very useful for observing changes on data model that drives SwifUI from outside of SwiftUI. Before you had to write the code like this to achieve that:

func startObservation() { withObservationTracking {

        print(store.state.toggle) // Here we have the new value (+ it is called once before any change)

    } onChange: {
        Task { startObservation() }
    }
}


I know that you get the new value; but in many cases (almost every case I encountered), that doesn't help. You mention

"This is very useful for observing changes on data model that drives SwifUI from outside of SwiftUI"

I disagree, because the model hasn't changed yet. That's the crippling aspect to it: You can't tell some controller object to recompute its state based on a change to another object in the model, because you're getting notified BEFORE that object has changed.

For example, if I have an object that represents a camera, and the user tweaks a value in the UI that changes its resolution, the camera might offer a different set of values for things like frame rate.

So if I get notified that the user changed the resolution, I can't then tell the Recorder object to recompute the remaining recording time based on the camera settings, because those settings will not have changed yet.

And incidentally, I've used startObservation() in places, and it's crippled by another idiotic design choice: It only works once. It reports the first change, and then never another one. So in the onChange closure, you have to re-start the observation. Every goddamned time.

It's another great example of serving only the most illogical and obscure use case. Who the hell would expect something called "startObservation" to just quit after one change? The stupidity is just galling.


> And incidentally, I've used startObservation() in places, and it's crippled by another idiotic design choice: It only works once. It reports the first change, and then never another one. So in the onChange closure, you have to re-start the observation. Every goddamned time.

The restart is there in my example, and it doesn't look too complex. And this aspect of observation is exactly what will be changed in Swift 6.2. Nice improvement, I'd say!

> I disagree, because the model hasn't changed yet. That's the crippling aspect to it: You can't tell some controller object to recompute its state based on a change to another object in the model, because you're getting notified BEFORE that object has changed.

If your model stores 2 interdependent states then you'll risk running into infinite update loop regardless of whether you've been notified from `didSet` or `willSet`. It can be fixed by changing the model.

To be fair I didn't understand your example with camera and resolution. In all cases you already know the new value and so the dependent code is good to go. The code shouldn't ever care where the value comes from: the main state, the future state, or some mock state.


There is a two parameter onChange modifier btw




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: