RxJava concatMap操作符

concatMap 作用

concatMap操作符和flatMap操作符非常类似。

下面是concatMap操作符的流程图:

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

如下代码:

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, 使用的线程都是不同的线程。

这也就解决了一个问题:既要是多个线程完成任务,又要保持任务的顺序。

转自:http://blog.csdn.net/johnny901114/article/details/51533282

延伸:
RxJava变换操作符:.concatMap( )与.flatMap( )的比较

你可能感兴趣的:(concatMap)