RxJava2极速入门——Rxjava操作符详解之合并操作符
Rxjava操作符详解合并操作符
最近公司事务比较繁忙,导致文章更新晚了,尽量保证在6月底的时候更新完RxJava2相关的东西。
好了接下来是正文的开编:
在RxJava中有这样一种操作符通过使用相关的修饰的操作符将两个Observable结合为一个新的Observable,然后将其数据进行发射其官网是这样描述的:Operators that work with multiple source Observables to create a single Observable
其分类如下:
Merge
Merge:Operators that work with multiple source Observables to create a single Observable
Merge操作主要讲多个可以观测的数据源合并成一个Observable然后发射
原理图如下:
这里主要以Merge以及MergeWith进行区分讲解先来看一段Base的示例代码
fun operatorsMerage(resultType: Int) {
val observableA = Observable
.interval(1, TimeUnit.SECONDS).skip(1)
.take(5).map {
it * 20 }
.subscribeOn(Schedulers.newThread())
val observabeB = Observable
.interval(2, TimeUnit.SECONDS)
.skip(1)
.take(1)
.repeat(2)
.subscribeOn(Schedulers.newThread())
//根据选择的Type展示不同操作符
when (resultType) {
0 -> {
Observable.merge(observableA, observabeB).subscribe(getBaseObsever("operators-Merage"))
}
1 -> {
observableA.mergeWith(observabeB).subscribe(getBaseObsever("operators-MerageWith"))
}
}
}
实现的效果图如下:
merge
在上述operatorsMerge(resultType: Int)这个方法中,分别创建不同时间段的整形定时队列,通过使用Observable.merge()这一静态方法将两者ObservableA与ObservableB合并在一起,注意merage不止是合并两个可以合并多个最多为四个如果需要更多可以使用mergeArray。
下边以示例代码对应的源码进行分析:
/**
* Flattens two ObservableSources into a single ObservableSource, without any transformation.
* You can combine items emitted by multiple ObservableSources so that they appear as a single ObservableSource, by
* using the {@code merge} method.
* @param source1
* an ObservableSource to be merged
* @param source2
* an ObservableSource to be merged
* @return an Observable that emits all of the items emitted by the source ObservableSources
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
* @see #mergeDelayError(ObservableSource, ObservableSource)
*/
@SuppressWarnings({
"unchecked", "rawtypes" })
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Observable<T> merge(ObservableSource<? extends T> source1, ObservableSource<? extends T> source2) {
ObjectHelper.requireNonNull(source1, "source1 is null");
ObjectHelper.requireNonNull(source2, "source2 is null");
return fromArray(source1, source2).flatMap((Function)Functions.identity(), false, 2);
}
/**
* Converts an Array into an ObservableSource that emits the items in the Array.
* @param items
* the array of elements
* @param <T>
* the type of items in the Array and the type of items to be emitted by the resulting ObservableSource
* @return an Observable that emits each item in the source Array
* @see <a href="http://reactivex.io/documentation/operators/from.html">ReactiveX operators documentation: From</a>
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
@NonNull
public static <T> Observable<T> fromArray(T... items) {
ObjectHelper.requireNonNull(items, "items is null");
if (items.length == 0) {
return empty();
} else
if (items.length == 1) {
return just(items[0]);
}
return RxJavaPlugins.onAssembly(new ObservableFromArray<T>(items));
}
/**
* Returns an identity function that simply returns its argument.
* @param <T> the input and output value type
* @return the identity function
*/
@SuppressWarnings("unchecked")
public static <T> Function<T, T> identity() {
return (Function<T, T>)IDENTITY;
}
从源码中可以得出Merge的底层基于两大核心,第一个核心在于ObservableFromArray,第二个核心在于flatMap,ObservableFromArray负责将多个数据源每一项数据遍历合并,而flatMap则负责将内部接口identity()的实现转化为实际需要发射数据的Observable
mergeWith
从上述示例代码中可以看出,实例化的Observable其实是可以使用mergeWith达到merge效果的,mergeWith其实就是将静态方法merge进行一层调用。源码如下:
/**
* Flattens this and another ObservableSource into a single ObservableSource, without any transformation.
* You can combine items emitted by multiple ObservableSources so that they appear as a single ObservableSource, by
* using the {@code mergeWith} method.
* @param other
* an ObservableSource to be merged
* @return an Observable that emits all of the items emitted by the source ObservableSources
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> mergeWith