Skip to content

vaderdan/Example2WayBinding

Repository files navigation

Rxswift Example showing Two Way Binding

This is the example playground repository proof of concept for this article:

Medium 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

Two way binding

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)

Instalation

This repository depends on Carthagepackage managed Link, head there and install it first

then from the main directory run

carthage update --platform iOS

About

RxSwift two way binding example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages