concatMap操作符的作用

concatMap操作符和flatMap操作符非常类似。如果对 flatMap操作符 不是很了解可以点击链接去看看我的上一篇博文。下面是concatMap操作符的流程图:

RxJava(四) concatMap操作符用法详解-LMLPHP

concatMap和flatMap最大的区别是concatMap发射的数据集是有序的,flatMap发射的数据集是无序的。

concatMap操作符的用法示例

通过上一篇对flatMap的介绍,我们的例子程序输出的结果是无序的。现在把代码中的flatMap换成concatMap:

Observable.from(Arrays.asList(
            "http://www.baidu.com/",
            "http://www.google.com/",
            "https://www.bing.com/"))
            .concatMap(new Func1<String, Observable<String>>() {
                    @Override
                    public Observable<String> call(String s) {
                        return createIpObservableMultiThread(s);
                    }
                })
            .observeOn(AndroidSchedulers.mainThread());

    // 获取ip
    private synchronized Observable<String> createIpObservableMultiThread(final String url) {
        return Observable
                .create(new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        try {
                            String ip = getIPByUrl(url);
                            printLog(tvLogs, "Emit Data -> ", url + "->" + ip);
                            subscriber.onNext(ip);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                            //subscriber.onError(e);
                            subscriber.onNext(null);
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                            //subscriber.onError(e);
                            subscriber.onNext(null);
                        }
                        subscriber.onCompleted();
                    }
                })
                .subscribeOn(Schedulers.io());
    }

输出结果:

Emit Data -> 'http://www.baidu.com/->115.239.211.112' , Main Thread:false, Thread Name:RxCachedThreadScheduler-1
Consume Data <- '115.239.211.112' , Main Thread:true, Thread Name:main
Emit Data -> 'http://www.google.com/->216.58.221.132'Main Thread:false, Thread Name:RxCachedThreadScheduler-3
Emit Data -> 'https://www.bing.com/->202.89.233.104' Main Thread:false, Thread Name:RxCachedThreadScheduler-2
Consume Data <- '216.58.221.132' Main Thread:true, Thread Name:main
Consume Data <- '202.89.233.104' Main Thread:true, Thread Name:main 

输出的结果始终都是baidu/google/bing, 使用的线程都是不同的线程。这也就解决了上篇博客提出的问题:既要是多个线程完成任务,又要保持任务的顺序

具体的细节 可以查看代码:github地址

05-08 08:08