How to split and join an observable

110 views
Skip to first unread message

Nathan Crouther

unread,
Sep 4, 2016, 12:56:09 AM9/4/16
to RxJava

I am trying to filter the set of emissions before flatmapping to an API call to prevent overloading the server, but I'd like the subscriber to get an onNext for each original event.  I debounce the original observable, and flatmap it to the API call (calling map on the inner observable to combine the input and output into a pair since the subscriber needs both.  Where I am getting stuck is on how to propagate to the subscriber the emissions that were filtered out before the flatmap.


Is there a idiomatic way of doing what I am trying to do?  combineLatest() seems similar to what I am trying to do, but I am not clear on how I would get the two observables to feed into it.  Does this look like a reasonable approach?


Observable<Location> initialObservable = getLocationObservable().share();


Observable<ApiResult> apiResultObservable = initialObservable

    .debounce(1, TimeUnit.MINUTES)

    .flatmap(l -> api.call(l.x, l.y));


Observable.combineLatest(initialObservable, apiResultObservable, Pair::create).subscribe(...)

chaim turkel

unread,
Sep 14, 2016, 2:27:55 AM9/14/16
to RxJava
I think you need to have two subscriptions one for the first stream and one for the last, then you can get both
chaim
Reply all
Reply to author
Forward
0 new messages