Swift Package Index


A Safer KVO


  • The latest stable release is 0.9.5. Released 2 months ago.
  • The last commit to main was 2 months ago.

Swift Version Compatibility

  • 0.9.5 and main
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.9.5 and main
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

KVObserver

Platforms SPM compatible License happn

A safer KVO.

Usage

Example:

let kvObserver = KVObserver()
let observedObject = ObservedObject()

/* Start observing observedObject */ 
let observingId = kvObserver.observe(object: observedObject, keyPath: #keyPath(ObservedObject.observableProperty), kvoOptions: [.initial], dispatchType: .asyncOnMainQueueDirectInitial, handler: { [weak self] change in
   /* Handle changes here */
})

/* End observing observedObject */ 
kvObserver.stopObserving(id: observingId)
/* Or, stop observing everything */
kvObserver.stopObservingEverything()

There are many dispatch types available, all of them with their subtle differences. Usually you’ll need the default one: .asyncOnMainQueueDirectInitial. This will dispatch the inital KVO firing synchronously (if there is one, that is if the .inital KVO option has been set), and further KVO firing asynchronously on the main thread.

Don’t hesitate to read the code to learn about the other dispatch types! All of the types are documented. There even are dispatch types specifically for CoreData.

TODO

  • Re-registration skipping
  • Support for Swift 4 KeyPath

Credits

This project was originally created by François Lamboley while working at happn.