Swift Pitch: Observation
There are already a few mechanisms for observation in Swift. Some of these include Key Value Observing (KVO), or
ObservableObject
; but those are relegated to in the case of KVO to justNSObject
descendants, andObservableObject
requires using Combine which is restricted to Darwin platforms and does not leverage language features like async/await orAsyncSequence
. By taking experience from those existing systems we can build a more generally useful feature that applies to all Swift reference types; not just those that inherit fromNSObject
and have it work cross platform with using the advantages from low level language features like async/await.[…]
Combine’s
ObservableObject
produces changes on the leading edge of the will/did events and all delivered values are before the value did set. Albeit this serves SwiftUI well, it is restrictive for non SwiftUI usage and can be surprising to developers first encountering that restriction.[…]
The
Observable
protocol includes a set of extension methods to handle observation. In the simplest, most common case, a client can use thechanges(for:)
method to observe changes to that field for a given instance. […] This allows users of this protocol to be able to observe the changes to specific values either as a distinct step in the chain of change events or as an asynchronous sequence of change events.[…]
By default the concept of observation is transitively forwarded; observing a keypath of
\A.b.c.d
means that if the field.b
isObservable
that is registered with an observer to track\B.c.d
and so on. This means that graphs of observations can be tracked such that any set of changes are forwarded as an event.[…]
The
ObservationTracking
mechanism is the primary interface designed for the purposes to interoperate with SwiftUI. Views will register via thewithTracking
method such that if in the execution ofbody
any field is accessed in anObservable
that field is registered into the access set that will be indicated in the handler passed to theaddChangeHandler
function. If at any point in time that handler needs to be directly invalidated theinvalidate
function can be invoked; which will remove all change handlers registered to theObservable
instances under the tracking.[…]
A default implementation can be accomplished in generality by a type wrapper that intercepts the modifications of any field on the
Observable
. The type wrapperDefaultObservable
provides default implementations for the type where the associatedObservation
type isObservationTracking.Token
. This means that developers have the flexibility of an easy to use interface that progressively allows for more and more detailed control: starting from mere annotation that grants general observability, progressing to delegation to a storage mechanism that manages registering and unregistering observers, to full control of observation.
There’s a number of issues we ran into with KVO, all around concurrency[…]
Philippe’s new
ObservationTracking
machinery reads like a shippable spiritual successor to that hack.