Defaults icon indicating copy to clipboard operation
Defaults copied to clipboard

Combine publisher methods should use `NSObject.KeyValueObservingPublisher`

Open sindresorhus opened this issue 4 years ago • 3 comments

https://developer.apple.com/documentation/objectivec/nsobject/keyvalueobservingpublisher

This should reduce some overhead. Right now, they're based on the callback API.


Eventually (when this package can target macOS 10.15), I imagine the callback APIs like Defaults.observe() could be based on the Combine publishers underneath too.


// @fredyshox In case you're interested in working on this.

sindresorhus avatar Mar 25 '20 04:03 sindresorhus

I was actually thinking about using this, but I wanted the API to be similar to standard observe: (same propagated KeyChange value). As far as I know, KeyValueObservingPublisher does not give [NSKeyValueChangeKey: Any] dictionary, just the new value.

fredyshox avatar Mar 26 '20 15:03 fredyshox

It does support the old option though. So there must be a way to get the old value.

By the way, the source for that publisher is now open source: https://github.com/apple/swift/commit/e7cd5ab17f5ce92315f0e23b269628ac2b9369ea#diff-bb784950aec7be6842901ecdef2bfbd5R44

sindresorhus avatar Mar 26 '20 16:03 sindresorhus

A possible solution here: https://stackoverflow.com/questions/60386000/how-to-use-combine-framework-nsobject-keyvalueobservingpublisher

sindresorhus avatar Jun 08 '20 19:06 sindresorhus

Closing as the Combine publishers will eventually be replaced by async stream ones.

sindresorhus avatar Nov 17 '22 11:11 sindresorhus