I have a question.
I need to get list of some items by a list of the item ids. At first, I tried
Observable.from(itemIds)
.flatMap(itemId -> requestToServer(itemId))
.subscribe(item -> { /* do something */ });
But the operator flatMap
does not guarantee the order of items.
I need to get item in order that ItemIds
have.
It would be great if there was the api like Promise.all()
.
Is there a way like Promise.all()
in RxJava? or any other ways?
It sounds like you're looking for the Zip operator
For example:
Observable<Integer> obs1 = Observable.just(1);
Observable<String> obs2 = Observable.just("Blah");
Observable<Boolean> obs3 = Observable.just(true);
Observable.zip(obs1, obs2, obs3, (Integer i, String s, Boolean b) -> i + " " + s + " " + b)
.subscribe(str -> System.out.println(str));
prints:
1 Blah true
Use concatMap instead. That will concatenate the emitted Observables in order rather than merging their emissions
Returns a new Observable that emits items resulting from applying a function that you supply to each item emitted by the source Observable, where that function returns an Observable, and then emitting the items that result from concatinating those resulting Observables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With