[Pitch] Observation (Revised)

Unfortunately, this is a limitation of using asynchronous sequences and I'm not sure if there's any elegant way around it. I tried to go into it a bit in the other thread.

To make this work how we want it, we need Observable to generate trailing edge (didSet) synchronous notifications.

SwiftUI works by creating an animation transaction which includes a before changes snapshot and an after changes snapshot. And critically it all needs to happen in the same event loop cycle – so no actor hops or async calls.

As it stands, SwiftUI can create its 'before changes' snapshot when it's triggered by the first leading edge willSet notification of the event loop.

However, in order for dependent observers to make any changes they need to make, we need trailing edge didSet observations to occur before the end of the current event loop cycle. Trailing edge, because we need to know what the changes are in order that we can respond to them appropriately.

Finally, once the event loop cycle has concluded, SwiftUI can generate its 'after changes' snapshot and everything works as expected.

I don't really see any other way it could work.

6 Likes