总结:
1、FlatMap 可以把一个发射器 Observable 通过某种方法转换为多个 Observables,然后再把这些分散的 Observables装进一个单一的发射器 Observable。但有个需要注意的是,flatMap 并不能保证事件的顺序
2、concatMap 与 FlatMap 的唯一区别就是 concatMap 保证了顺序
一、FlatMap语法
public Observable<Integer> getRxJavaCreateExampleData() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(ObservableEmitter<Integer> emitter) {
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 1);
emitter.onNext(1);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 2);
emitter.onNext(2);
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 3);
emitter.onNext(3);
emitter.onComplete();
LogUtils.debug(TAG, "getRxJavaCreateExampleData---:" + Thread.currentThread().getName() + "--:" + 4);
emitter.onNext(4);
}
});
}
public void rxJavaFlatMapExample() {
Disposable disposable = model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.flatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaFlatMapExample--flatmap--:" + Thread.currentThread().getName() + "--:" + integer);
List<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("this is flatMap--" + integer + "---i--:" + i);
}
long delayTime = 1 + (int) (10 * Math.random());
return Observable.fromIterable(list).delay(delayTime, TimeUnit.SECONDS);
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
LogUtils.error(TAG, "rxJavaFlatMapExample--Consumer--:" + Thread.currentThread().getName() + "--:" + s);
}
});
compositeDisposable.add(disposable);
}
日志
08-08 17:28:30.149 15730-16292/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-2--:1
08-08 17:28:30.149 15730-16292/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--flatmap--:RxCachedThreadScheduler-2--:1
08-08 17:28:30.150 15730-16292/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-2--:2
08-08 17:28:30.150 15730-16292/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--flatmap--:RxCachedThreadScheduler-2--:2
08-08 17:28:30.151 15730-16292/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-2--:3
08-08 17:28:30.151 15730-16292/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--flatmap--:RxCachedThreadScheduler-2--:3
08-08 17:28:30.152 15730-16292/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-2--:4
08-08 17:28:31.154 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--3---i--:0
rxJavaFlatMapExample--Consumer--:main--:this is flatMap--3---i--:1
08-08 17:28:31.155 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--3---i--:2
08-08 17:28:36.150 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--1---i--:0
08-08 17:28:36.151 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--1---i--:1
rxJavaFlatMapExample--Consumer--:main--:this is flatMap--1---i--:2
08-08 17:28:38.153 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--2---i--:0
08-08 17:28:38.154 15730-15730/com.example.zhang E/MainPresenter: rxJavaFlatMapExample--Consumer--:main--:this is flatMap--2---i--:1
rxJavaFlatMapExample--Consumer--:main--:this is flatMap--2---i--:2
二、ConcatMap语法练习
public void rxJavaConcatMapExample() {
Disposable disposable = model.getRxJavaCreateExampleData()
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.concatMap(new Function<Integer, ObservableSource<String>>() {
@Override
public ObservableSource<String> apply(Integer integer) throws Exception {
LogUtils.error(TAG, "rxJavaConcatMapExample--flatmap--:" + Thread.currentThread().getName() + "--:" + integer);
List<String> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add("this is ConcatMap--" + integer + "---i--:" + i);
}
long delayTime = 1 + (int) (10 * Math.random());
return Observable.fromIterable(list).delay(delayTime, TimeUnit.SECONDS);//delay方法内部切换了线程
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
LogUtils.error(TAG, "rxJavaConcatMapExample--Consumer--:" + Thread.currentThread().getName() + "--:" + s);
}
});
compositeDisposable.add(disposable);
}
日志
08-08 17:37:17.486 18155-18519/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:1
08-08 17:37:17.487 18155-18519/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--flatmap--:RxCachedThreadScheduler-1--:1
08-08 17:37:17.492 18155-18519/com.example.zhang D/MainModel: getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:2
getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:3
getRxJavaCreateExampleData---:RxCachedThreadScheduler-1--:4
08-08 17:37:23.494 18155-18520/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--flatmap--:RxComputationThreadPool-1--:2
08-08 17:37:23.494 18155-18155/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--1---i--:0
rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--1---i--:1
rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--1---i--:2
08-08 17:37:25.496 18155-18155/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--2---i--:0
08-08 17:37:25.501 18155-18155/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--2---i--:1
08-08 17:37:25.501 18155-18527/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--flatmap--:RxComputationThreadPool-2--:3
08-08 17:37:25.501 18155-18155/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--2---i--:2
08-08 17:37:32.504 18155-18155/com.example.zhang E/MainPresenter: rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--3---i--:0
rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--3---i--:1
rxJavaConcatMapExample--Consumer--:main--:this is ConcatMap--3---i--:2