Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift - withLatestFrom combining values from both observables

I want to achieve result like this:

L -1-2-3------4------5-6-7-8----
R ---------A------B----------C--

O ---------A3-----B4---------C8

So basically something like withLatestFrom but combining values from both observables (like combine latest).

I guess there is no ready operator for that. Any idea how to achieve this?

like image 227
Wujo Avatar asked Nov 03 '17 14:11

Wujo


2 Answers

Just use resulting selector from your withLatestFrom. The overloaded implementation without closure simply ignores first observable. For example:

Observable.just("one")
  .withLatestFrom(Observable.just(1)) 
  { oneAsString, oneAsInt in return (oneAsString, oneAsInt) }
like image 125
Timofey Solonin Avatar answered Oct 01 '22 03:10

Timofey Solonin


Short version where your R = input.r and L = input.l

let output = input.r
.withLatestFrom(input.l) { ($0, $1) }
like image 33
milczi Avatar answered Oct 01 '22 01:10

milczi