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。