Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift Use withLatestFrom operator with multiple sources

I have 3 observables, namely source, source1 and source2. What I want is whenever source emits a distinct event to get the value of source1 and source2. This is the code I've come up with, obviously it won't compile since withLatestFrom expects only one observable.

source.distinctUntilChanged()
    .withLatestFrom(source1, source2) { ($0, $1.0, $1.1) }
    .subscribe(onNext: { (A, B, C) in
        print("OnNext called")
    })
    .disposed(by: bag)
like image 489
Daniyal Raza Avatar asked Oct 25 '25 00:10

Daniyal Raza


1 Answers

You almost have it. How about just combining source1 and source2?

source.distinctUntilChanged()
    .withLatestFrom(Observable.combineLatest(source1, source2)) { ($0, $1.0, $1.1) }
    .subscribe(onNext: { (A, B, C) in
        print("OnNext called")
    })
    .disposed(by: bag)
like image 152
Daniel T. Avatar answered Oct 26 '25 16:10

Daniel T.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!