RxJava2.x-window语法
2018-08-19 本文已影响163人
河马过河
一、window语法
public Observable<Integer> getRxJavaDistinctData() {
return Observable.just(1, 2, 2, 1, 1, 2, 3, 4, 5, 2);
}
public void rxJavaWindowExample() {
Disposable disposable = model.getRxJavaDistinctData()
.window(3)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Observable<Integer>>() {
@Override
public void accept(Observable<Integer> longObservable) throws Exception {
LogUtils.error(TAG, "rxJavaReduceExample--:" + Thread.currentThread().getName() + "-consumer1-:" );
Disposable disposable1 = longObservable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaReduceExample--:" + Thread.currentThread().getName() + "-consumer2-:" + integer);
}
});
compositeDisposable.add(disposable1);
}
});
compositeDisposable.add(disposable);
}
日志
08-19 15:59:53.646 3730-3730/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer1-:
08-19 15:59:53.647 3730-3730/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer1-:
rxJavaReduceExample--:main-consumer1-:
rxJavaReduceExample--:main-consumer1-:
08-19 15:59:53.648 3730-3730/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:3
rxJavaReduceExample--:main-consumer2-:4
rxJavaReduceExample--:main-consumer2-:5
rxJavaReduceExample--:main-consumer2-:2
二、window方法2
public void rxJavaWindowExample() {
Disposable disposable = model.getRxJavaDistinctData()
.window(3,2)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Observable<Integer>>() {
@Override
public void accept(Observable<Integer> longObservable) throws Exception {
LogUtils.error(TAG, "rxJavaReduceExample--:" + Thread.currentThread().getName() + "-consumer1-:" );
Disposable disposable1 = longObservable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaReduceExample--:" + Thread.currentThread().getName() + "-consumer2-:" + integer);
}
});
compositeDisposable.add(disposable1);
}
});
compositeDisposable.add(disposable);
}
日志
08-19 15:57:29.906 28739-28739/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer1-:
rxJavaReduceExample--:main-consumer1-:
08-19 15:57:29.908 28739-28739/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer1-:
rxJavaReduceExample--:main-consumer1-:
08-19 15:57:29.909 28739-28739/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer1-:
08-19 15:57:29.910 28739-28739/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:2
rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:1
rxJavaReduceExample--:main-consumer2-:2
08-19 15:57:29.911 28739-28739/com.example.zhang E/MainPresenter: rxJavaReduceExample--:main-consumer2-:3
rxJavaReduceExample--:main-consumer2-:3
rxJavaReduceExample--:main-consumer2-:4
rxJavaReduceExample--:main-consumer2-:5
rxJavaReduceExample--:main-consumer2-:5
rxJavaReduceExample--:main-consumer2-:2
总结
1、window(long count) 被分割成的每个window最大数据值
2、window(long count, long skip) count 每个window最大数据值,skip步长
3、window与buffer区别:window是把数据分割成了Observable,buffer是把数据分割成List
河马过河微信公众号.jpg