RxJava转换操作符 conactMap

concatMap操作符是将源Observable转换成对各操作符,然后又链接返回给源Observable,这与flatMap十分相似,最大的区别:concatMap 是保证结果的顺序性,就是顺序与输入一致.

例子:在这里最大的数线程延迟最长,照理应该是最大的数最后出来,结果很意外:

<pre name="code" class="java"> Observable.just(2, 1).concatMap(new Func1<Integer, Observable<Integer>>() {
            @Override
            public Observable<Integer> call(final Integer integer) {
                return Observable.create(new Observable.OnSubscribe<Integer>() {
                    @Override
                    public void call(Subscriber<? super Integer> subscriber) {
                        if (subscriber.isUnsubscribed()) return;
                        try {
                            LogUtils.d("------>thread:" + Thread.currentThread().getName());
                            Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(integer));
                            subscriber.onNext(integer);
                            subscriber.onCompleted();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                }).subscribeOn(Schedulers.newThread());
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {
                LogUtils.d("------>onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.d("------>onError()" + e);
            }

            @Override
            public void onNext(Integer aLong) {
                LogUtils.d("------>onNext:" + aLong);
            }
        });

结果:

 
 
02-26 15:22:03.028 25684-25832/? D/----->: ------>thread:RxNewThreadScheduler-1
02-26 15:22:05.028 25684-25832/? D/----->: ------>onNext:2
02-26 15:22:05.038 25684-25864/? D/----->: ------>thread:RxNewThreadScheduler-2
02-26 15:22:06.038 25684-25864/? D/----->: ------>onNext:1
02-26 15:22:06.038 25684-25864/? D/----->: ------>onCompleted()

这更加说明了concatMap 的顺序保障

同时区分faltMap:这里用flatMap演示:

  Observable.just(2, 1).flatMap(new Func1<Integer, Observable<Integer>>() {
            @Override
            public Observable<Integer> call(final Integer integer) {
                return Observable.create(new Observable.OnSubscribe<Integer>() {
                    @Override
                    public void call(Subscriber<? super Integer> subscriber) {
                        if (subscriber.isUnsubscribed()) return;
                        try {
                            LogUtils.d("------>thread:" + Thread.currentThread().getName());
                            Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(integer));
                            subscriber.onNext(integer);
                            subscriber.onCompleted();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                }).subscribeOn(Schedulers.newThread());
            }
        }).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {
                LogUtils.d("------>onCompleted()");
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.d("------>onError()" + e);
            }

            @Override
            public void onNext(Integer aLong) {
                LogUtils.d("------>onNext:" + aLong);
            }
        });

结果:

02-26 15:23:07.108 26612-26795/com.rxandroid.test1 D/----->: ------>thread:RxNewThreadScheduler-1
02-26 15:23:07.108 26612-26796/com.rxandroid.test1 D/----->: ------>thread:RxNewThreadScheduler-2
02-26 15:23:08.108 26612-26796/com.rxandroid.test1 D/----->: ------>onNext:1
02-26 15:23:09.108 26612-26795/com.rxandroid.test1 D/----->: ------>onNext:2
02-26 15:23:09.108 26612-26795/com.rxandroid.test1 D/----->: ------>onCompleted()

在这里,flatmap 并未保证顺序性!



你可能感兴趣的:(RxJava转换操作符 conactMap)