Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava - zip list of Observable

I have a list of Observables (RxJava 1).

List<Observable> observableList = new ArrayList<>();

It can contain at least 1 Observable. Each has the same type of the result.

How can I zip results of the all Observables?

I thought about zip-operator but it doesn't support List and I don't know quantity of observables (it can be 1,2,3,4....)

like image 297
asf Avatar asked Apr 20 '17 16:04

asf


1 Answers

You can use the static zip(java.lang.Iterable<? extends Observable<?>> ws,FuncN<? extends R> zipFunction) method.

It is a zip method that takes an Iterable of Observables and a FuncN (which takes a varargs parameter for its call method) and uses it to combine the corresponding emitted Objects into the result to be omitted by the new returned Observable.

So for example you could do:

Observable.zip(observableList, new FuncN(){
    public ReturnType call(java.lang.Object... args){
        ReturnType result; //to be made
        //preparatory code for using the args
        for (Object obj : args){
            ReturnType retObj = (ReturnType)obj;
            //code to use the arg once at a time to combine N of them into one.
        }
        return result;
    }
});
like image 164
et_l Avatar answered Sep 20 '22 16:09

et_l