2017-10-09 238 views
1

我很努力地找到任何将两个Flowable合并成一个的RxJava2实例。RxJava2将两个Flowables合并为一个

我想修改​​包括沿

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(); 

编辑线的东西:我试图从源代码和anothersource采用整数并将它们加起来但似乎从RxJava1方式根本不同做这些......我尝试过一堆返回Integer,Publisher,Flowable和void的变体,但是在zip运算符本身中一直在Eclipse中发生错误。

我无法找出去的地方在.zip(Iterable<? extends Publisher<? extends T>>, Function<? super Object[], ? extends R>).

+0

你期待什么样的结果,做什么结果,你有现在? – Benjamin

+0

尝试'BiFunction '并调整'apply'方法的类型。压缩函数不会获得源Flowful,而是每个调用的一个值。 – akarnokd

+0

谢谢@akarnokd--这正是我的误解。 – ChopperOnDick

回答

0

既然你只需要压缩2个悬浮剂,你可以使用Flowable.zipWith操作。

它的使用方式是如下:

source.zipWith(anotherSource, new BiFunction<Integer, Integer, Integer>() { 
    @Override public Integer apply(Integer a, Integer b) { 
     return a + b; 
    } 
};