Multi-path KVO

I keep finding myself reaching for the original context-pointer-based API for Cocoa key-value observing instead of the key-path based Swift one when I want to perform exactly the same thing for multiple observations, rather than creating seemingly duplicate objects all doing the same thing.

One example of such a scenario is reflecting the values of Progress in a UI component, which is not as simple as keyPathsForValuesAffecting<Blah>, but needs to execute the same code path each time. I would also be seeking additional examples to round out the proposal as I write it.

Extending the implementation with a variant that accepts [PartialKeyPath<Self>] seems like a simple enough addition, but must be done in the stdlib due to the use of stdlib-private machinery.

A working implementation can be seen here. (4.2 here.)

It is a non-goal of this pitch to outline the far-off space future (:rocket:) of observation. It strictly seeks to make an additive quality-of-life improvement to the status quo. I certainly want to allow room for discussing alternatives, but I also want to avoid the thread derailing immediately.

3 Likes

cc @David_Smith

Offhand, this seems like a reasonable enhancement to consider in the 5.0 timeframe. Thanks for your work on this Zachary!

1 Like