This is the example playground repository proof of concept for this article:
Usually when we use RxSwift, we setup things in a way that one part of the code emits events (for example: TextField onchange text) and other parts listen for it aka. observe changes (for example: UILable that shows text)
But how about passing events in both directions like so:
TextField <------> Observable <------> TextField
We can do this by bind two RxSwif observables to listen for each other's changes
infix operator <->
func <-> (lhs: BehaviorRelay, rhs: BehaviorRelay) -> Disposable {
typealias ItemType = (current: T, previous: T)
return Observable.combineLatest(lhs.currentAndPrevious(), rhs.currentAndPrevious())
.filter({ (first: ItemType, second: ItemType) -> Bool in
return first.current != second.current
})
.subscribe(onNext: { (first: ItemType, second: ItemType) in
if first.current != first.previous {
rhs.accept(first.current)
}
else if (second.current != second.previous) {
lhs.accept(second.current)
}
})
}
let textFirst = BehaviorRelay(value: nil)
let textSecond = BehaviorRelay(value: nil)
(textSecond <-> textFirst).disposed(by: disposeBag)
(You can check the BasicFormController.swift or FullFormController.swift for the full code)
This repository depends on Carthage
package managed Link, head there and install it first
then from the main directory run
carthage update --platform iOS