Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2 zip two Flowables into one

Tags:

I am struggling to find any RxJava2 examples of zipping two Flowables into one.

I am trying to modify this test to include something along the lines of

    Integer[] ints = new Integer[count];
    Integer[] moreints = new Integer[count];
    Arrays.fill(ints, 777);
    Arrays.fill(moreints, 777);

    Flowable<Integer> source = Flowable.fromArray(ints);
    Flowable<Integer> anothersource = Flowable.fromArray(moreints);

    Flowable<Integer> zippedsources = Flowable.zip(source, anothersource,
            new BiFunction<Flowable<Integer>, Flowable<Integer>, Flowable<Integer>>() {

                @Override
                public void apply(Flowable<Integer> arg0, Flowable<Integer> arg1) throws Exception {
                    return arg0.blockingFirst() + arg1.blockingLast();
                }

    }).runOn(Schedulers.computation()).map(this).sequential();

Edit: I am trying to take an Integer from source and anothersource and add them up but it seems fundamentally different from the RxJava1 way of doing that ... I have tried a bunch of variations returning Integer, Publisher, Flowable and void but keep getting and error in Eclipse on the zip operator itself.

I am unable to figure out what goes where in .zip(Iterable<? extends Publisher<? extends T>>, Function<? super Object[], ? extends R>).

like image 898
ChopperOnDick Avatar asked Oct 09 '17 15:10

ChopperOnDick


1 Answers

Since you only need to zip two flowables, you could use Flowable.zipWith Operator.

The way it is used is as following:

source.zipWith(anotherSource, new BiFunction<Integer, Integer, Integer>() {
    @Override public Integer apply(Integer a, Integer b) {
        return a + b;
    } 
};
like image 116
Andri Ihsannudin Avatar answered Sep 29 '22 23:09

Andri Ihsannudin