Skip to content

crmitchelmore/Observable

 
 

Repository files navigation

Observable

Observable is the easiest way to observe values in Swift.

How to

Create an Observable

var position = Observable(CGPoint.zero)

Create Observer and ImmutableObserver

var position = Observable(CGPoint.zero)
var immutablePosition: ImmutableObservable<CGPoint> = position 
// With an ImmutableObservable the value can't be changed, only read or observe it's value changes

Add an observer

position.observe { p in
    // handle new position
}

Add an observer and specify the DispatchQueue

position.observe(DispatchQueue.main) { p in
// handle new position
}

Change the value

position.value = p

Memory management

For a single observer you can store the returned Disposable to a variable

disposable = position.observe { p in

For multiple observers you can add the disposable to a Disposal variable

position.observe { }.add(to: &disposal)

And always weakify self when referencing self inside your observer

position.observe { [weak self] position in

Installation

CocoaPods

Observable is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Observable'

Carthage

Observable is available through Carthage. To install it, simply add the following line to your Cartfile:

github "roberthein/Observable" "master"

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

About

The easiest way to observe values in Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 89.2%
  • Ruby 10.8%