RxJava2 / RxJava2操作符scan
scan操作符将Observable的结果在BiFunction扫描一遍后交给Observer使用,scan最大的功用是在BiFunction里面的apply里面做一次计算,有条件、有筛选的输出最终结果,例如代码例子:
package zhangphil.app;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
test();
}
private void test() {
DisposableObserver disposableObserver = new DisposableObserver<Integer>() {
@Override
public void onNext(Integer i) {
Log.d(TAG, "#####开始#####");
Log.d(TAG + "数据", String.valueOf(i));
Log.d(TAG, "#####结束#####");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
mCompositeDisposable.add(
getObservable().scan(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer a, Integer b) throws Exception {
Log.d(TAG, "scan:" + a + "," + b);
return b * b; //这个结果将作为最终输出结果到onNext里面
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(disposableObserver));
}
private Observable<Integer> getObservable() {
return Observable.range(1, 5);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 如果退出程序,就清除后台任务
mCompositeDisposable.clear();
}
}
代码输出:
05-15 16:41:21.898 18230-30693/zhangphil.app D/MainActivity: scan:1,2
05-15 16:41:21.898 18230-30693/zhangphil.app D/MainActivity: scan:4,3
05-15 16:41:21.898 18230-30693/zhangphil.app D/MainActivity: scan:9,4
05-15 16:41:21.898 18230-30693/zhangphil.app D/MainActivity: scan:16,5
05-15 16:41:21.947 18230-18230/zhangphil.app D/InputTransport: Input channel constructed: fd=73
05-15 16:41:21.947 18230-18230/zhangphil.app D/ViewRootImpl@9990594[MainActivity]: setView = DecorView@a52a83d[MainActivity] touchMode=true
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####开始#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity数据: 1
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####结束#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####开始#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity数据: 4
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####结束#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####开始#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity数据: 9
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####结束#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####开始#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity数据: 16
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####结束#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####开始#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity数据: 25
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: #####结束#####
05-15 16:41:21.949 18230-18230/zhangphil.app D/MainActivity: onComplete
源输入:1,2,3,4,5
scan首先把源输入1,2,3,4,5数据成对成对的扫描一次,然后根据apply的约束函数输出一个结果形成一个最终输出结果给Observer。
注意:由于在第一次scan时候,源输入队列中的数据只有“1”,那么就只返回一个“1”。后面又成对的数据源,则成对的扫描。