romanilchyshyn romanilchyshyn - 11 days ago 5
iOS Question

How to bind multiple observers to one ControlProperty

I need to bind

slider.rx.value
to 2 observers with different mappings.

slider.rx.value.map { [unowned self] in self.formatter.string(from: NSNumber(value: $0)) ?? "" }
.bindTo(textFieldAlpha.rx.text)
.addDisposableTo(disposeBag)

slider.rx.value.map { Enhance.Global(alpha: $0) }
.bindTo(enhance)
.addDisposableTo(disposeBag)


But i seems that only last binding works. How to achieve this?

Answer

Does this accomplish what you want?

let observable = slider.rx.value.shareReplay(1)

observable.map  { [unowned self] in self.formatter.string(from: NSNumber(value: $0)) ?? "" }
    .bindTo(textFieldAlpha.rx.text)
    .addDisposableTo(disposeBag)

observable.map { Enhance.Global(alpha: $0) }
    .bindTo(enhance)
    .addDisposableTo(disposeBag)

Here is also a link to a nice cheat sheet for RxSwift https://www.cheatography.com/donghua-li/cheat-sheets/rxswift-operators/

Comments