Search code examples
iosswiftobservablerx-swift

How do I observe two rx sequences and subscribe with two closure arguments?


I want to observe two behaviorRelays with a single observer, wait for both relays to emitt their values, then in the subscription have two seperate closure arguemts, one for each relay. Something like this:

let one = firmwareService.basicIODeviceUnit.compactMap { $0?.canBeUpdated }
let two = firmwareService.motorDeviceUnit.compactMap { $0?.canBeUpdated }
        
Observable.of(one, two).flatMap{ $0 }.subscribe(onNext: { a, b in
    print("--", a, b)
}).disposed(by: disposeBag)

The above code isn't allowed. The operators like merge or zip seem to bundle both relays into a single closure argumet so I guess they won't work. What do I use?

I have looked through this thread, so it should be possible, but I can't wrap my head around it since I use swift RxJS Subscribe with two arguments


Solution

  • I'm not sure what you mean because zip does exactly what you want. So does combineLatest.

    let one = firmwareService.basicIODeviceUnit.compactMap { $0?.canBeUpdated }
    let two = firmwareService.motorDeviceUnit.compactMap { $0?.canBeUpdated }
    
    Observable.zip(one, two)
        .subscribe(onNext: { a, b in
            print("--", a, b)
        })
        .disposed(by: disposeBag)