RxJava 操作符 combineLatest

54 篇文章 0 订阅
20 篇文章 0 订阅


 combineLatest


/**
 * Combines two source Observables by emitting an item that aggregates the latest values of each of the
 * source Observables each time an item is received from either of the source Observables, where this
 * aggregation is defined by a specified function.
 * <p>
 * <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/combineLatest.png" alt="">
 * <dl>
 *  <dt><b>Scheduler:</b></dt>
 *  <dd>{@code combineLatest} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @param o1
 *            the first source Observable
 * @param o2
 *            the second source Observable
 * @param combineFunction
 *            the aggregation function used to combine the items emitted by the source Observables
 * @return an Observable that emits items that are the result of combining the items emitted by the source
 *         Observables by means of the given aggregation function
 * @see <a href="http://reactivex.io/documentation/operators/combinelatest.html">ReactiveX operators documentation: CombineLatest</a>
 */
@SuppressWarnings("unchecked")
public static final <T1, T2, R> Observable<R> combineLatest(Observable<? extends T1> o1, Observable<? extends T2> o2, Func2<? super T1, ? super T2, ? extends R> combineFunction) {
    return combineLatest(Arrays.asList(o1, o2), Functions.fromFunc(combineFunction));
}

可以看到该方法是将两个数据源的释放item放到一起通过一个func(某种方法)返回你想要的东西,测试代码如下:

private void test() {
    Observable<String> o1 = Observable.create(new Observable.OnSubscribe<String>() {
      @Override public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("o1");
        subscriber.onNext("o2");
        subscriber.onNext("o3");
      }
    });
    Observable<String> o2 = Observable.create(new Observable.OnSubscribe<String>() {
      @Override public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("o4");
        subscriber.onNext("o5");
        subscriber.onNext("o6");
      }
    });
    Observable.combineLatest(o2, o1, new Func2<String, String, String>() {
      @Override public String call(String s, String s2) {
        Log.e("combine --- >", "s = " + s + " | s2 = " + s2);
        return s + s2;
      }
    }).subscribe(new Observer<String>() {
      @Override public void onCompleted() {
        Log.e("onCompleted --- >", "onCompleted");
      }

      @Override public void onError(Throwable e) {
        Log.e("onError --- >", "onError");

      }

      @Override public void onNext(String o) {
        Log.e("onNext --- >", o);
      }
    });
  }

需要注意的是:

1 如果没有订阅者,那么什么都不会执行,正好符合了rx的一些基本要领。

2 打出日志如下:

04-20 20:45:02.194  14076-14076/com.fernandocejas.android10.sample.presentation E/combine --- >﹕ s = o6 | s2 = o1
04-20 20:45:02.195  14076-14076/com.fernandocejas.android10.sample.presentation E/onNext --- >﹕ o6o1
04-20 20:45:02.195  14076-14076/com.fernandocejas.android10.sample.presentation E/combine --- >﹕ s = o6 | s2 = o2
04-20 20:45:02.195  14076-14076/com.fernandocejas.android10.sample.presentation E/onNext --- >﹕ o6o2
04-20 20:45:02.195  14076-14076/com.fernandocejas.android10.sample.presentation E/combine --- >﹕ s = o6 | s2 = o3
04-20 20:45:02.195  14076-14076/com.fernandocejas.android10.sample.presentation E/onNext --- >﹕ o6o3

3 另外一个点是o1和o2的顺序会对结果有影响,上面的日志也充分说明了这点,s的值一开始就是"o6"而s2的值却执行了多次直到最后释放出o3。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值