RxJava2 Flowable concatMap(变换操作符)

目录

concatMap系列(变换操作符)

concatMap接口

concatMap系列图解

concatMap测试用例

concatMap测试用例说明


concatMap系列变换操作符)

concatMap接口

<R> Flowable<R> concatMap(Function<? super T,? extends Publisher<? extends R>> mapper)

Returns a new Flowable that emits items resulting from applying a function that you supply to each item emitted by the source Publisher, where that function returns a Publisher, and then emitting the items that result from concatenating those resulting Publishers.(这个英文是一个长句,蛮有意思,很考验翻译功底)

返回一个新的Flowable,它发出的项目是一个函数提供的结果,这个函数是你为源Publisher发射的每一个项目所提供的函数,函数返回一个Publisher,然后发射项目,这些项目是连接这些从函数中生成的Publisher所产生的项目。

(再提炼一下,如果在翻译的时候一眼看不明白就分拆翻译)

返回一个新的Flowable,该Flowable发出的是您为源Publisher发出的每一个项提供的函数所生成的项目,函数返回是一个Publisher,然后发出连接这些从函数中生成的Publisher所产生的项。

<R> Flowable<R> concatMap(Function<? super T,? extends Publisher<? extends R>> mapper, int prefetch)

返回一个新的Flowable,该Flowable发出的是您为源Publisher发出的每一个项提供的函数所生成的项目,函数返回是一个Publisher,然后发出连接这些从函数中生成的Publisher所产生的项。prefetch是从当前Flowable预取的元素数

<R> Flowable<R> concatMapDelayError(Function<? super T,? extends Publisher<? extends R>> mapper)

Maps each of the items into a Publisher, subscribes to them one after the other, one at a time and emits their values in order while delaying any error from either this or any of the inner Publishers till all of them terminate.

将每个项目映射到发布服务器,一个接一个地订阅它们,一次一个地按顺序发出它们的值,同时延迟来自这个或任何内部发布者的任何错误,直到它们全部终止。

<R> Flowable<R> concatMapDelayError(Function<? super T,? extends Publisher<? extends R>> mapper, int prefetch, boolean tillTheEnd)

Maps each of the items into a Publisher, subscribes to them one after the other, one at a time and emits their values in order while delaying any error from either this or any of the inner Publishers till all of them terminate.

将每个项目映射到发布服务器,一个接一个地订阅它们,一次一个地按顺序发出它们的值,同时延迟来自这个或任何内部发布者的任何错误,直到它们全部终止。

<R> Flowable<R> concatMapEager(Function<? super T,? extends Publisher<? extends R>> mapper)

Maps a sequence of values into Publishers and concatenates these Publishers eagerly into a single Publisher.

将一系列值映射到Publishers中,并将这些Publisher急切地连接到一个单独的Publisher中。

<R> Flowable<R> concatMapEager(Function<? super T,? extends Publisher<? extends R>> mapper, int maxConcurrency, int prefetch)

Maps a sequence of values into Publishers and concatenates these Publishers eagerly into a single Publisher.

将一系列值映射到Publishers中,并将这些Publishers急切地连接到一个Publisher中。

<R> Flowable<R> concatMapEagerDelayError(Function<? super T,? extends Publisher<? extends R>> mapper, boolean tillTheEnd)

Maps a sequence of values into Publishers and concatenates these Publishers eagerly into a single Publisher.

将一系列值映射到Publishers中,并将这些Publishers急切地连接到一个Publisher中。

<R> Flowable<R> concatMapEagerDelayError(Function<? super T,? extends Publisher<? extends R>> mapper, int maxConcurrency, int prefetch, boolean tillTheEnd)

Maps a sequence of values into Publishers and concatenates these Publishers eagerly into a single Publisher.

将一系列值映射到Publishers中,并将这些Publishers急切地连接到一个Publisher中。

<U> Flowable<U> concatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper)

Returns a Flowable that concatenate each item emitted by the source Publisher with the values in an Iterable corresponding to that item that is generated by a selector.

返回一个Flowable,它将源Publisher发出的每个项目与Iterable中与选择器生成的项目对应的值相连接。

<U> Flowable<U> concatMapIterable(Function<? super T,? extends Iterable<? extends U>> mapper, int prefetch)

Returns a Flowable that concatenate each item emitted by the source Publisher with the values in an Iterable corresponding to that item that is generated by a selector.

返回一个Flowable,它将源Publisher发出的每个项目与Iterable中与选择器生成的项目对应的值相连接。

  注意:官方文档上可能还有concatMapCompletable、concatMapMaybe等一系列但是在rxjava:2.0.1的源码中是没有的

concatMap系列图解

这里concatMap和flatmap具有相似的原理所有官方也给出的是一样的图解,途中很形象的将各种颜色不同圆形的项目根据FlatMap的规则变换成了菱形,恰好印证了给源Publisher发射的每一个item提供函数,返回一个Publisher,并发射项目。FlatMap中的圆形到方形的映射可以看成是一种函数关系,特别形象。

concatMap测试用例

concatMap测试代码
@Test
    public void concatMap() {
        System.out.println("######concatMap#####");
        Flowable flowable = Flowable.just(30, 80, 90).concatMap(new Function<Integer, Publisher<?>>() {
            @Override
            public Publisher<String> apply(Integer integer) throws Exception {
                System.out.println("这里是源Publisher(Flowable)发射的项:" + integer);
                return Flowable.just(integer + "#");
            }
        });

        flowable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String item) throws Exception {
                System.out.println("这里是指定函数返回的Publisher发射的项:" + item);
            }
        });
    }


concatMap测试结果:
######concatMap#####
这里是源Publisher(Flowable)发射的项:30
这里是指定函数返回的Publisher发射的项:30#
这里是源Publisher(Flowable)发射的项:80
这里是指定函数返回的Publisher发射的项:80#
这里是源Publisher(Flowable)发射的项:90
这里是指定函数返回的Publisher发射的项:90#


#concatMapIterable测试代码#
@Test
    public void concatMapIterable() {
        System.out.println("######concatMapIterable#####");
        Flowable.just(3, 2, 4, 6, 8, 9).concatMapIterable(new Function<Integer, Iterable<String>>() {
            @Override
            public Iterable<String> apply(Integer item) throws Exception {
                System.out.println("这里是源Publisher(Flowable)发射的项,item:" + item);
                List<String> list = new ArrayList<String>();
                list.add(item + "*");
                return list;
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println("这里是指定函数返回的Publisher发射的项, s:" + s);
            }
        });
    }
concatMapIterable测试结果
######concatMapIterable#####
这里是源Publisher(Flowable)发射的项,item:3
这里是指定函数返回的Publisher发射的项, s:3*
这里是源Publisher(Flowable)发射的项,item:2
这里是指定函数返回的Publisher发射的项, s:2*
这里是源Publisher(Flowable)发射的项,item:4
这里是指定函数返回的Publisher发射的项, s:4*
这里是源Publisher(Flowable)发射的项,item:6
这里是指定函数返回的Publisher发射的项, s:6*
这里是源Publisher(Flowable)发射的项,item:8
这里是指定函数返回的Publisher发射的项, s:8*
这里是源Publisher(Flowable)发射的项,item:9
这里是指定函数返回的Publisher发射的项, s:9*



concatMap测试用例说明

concatMap测试用例只是使用了最简单的一个接口作说明测试,其他的基本也都是这个原理,测试的打印结果也是一样的,看不出具体的不同,但是这要结合实际场景落地侧能真正的看出细节上的差别,后续会写落地场景相关的例子。

concatMapDelayError:在发射项目的过程中出现error的话,胡继续发射下一项,直到完毕,最后才处理error问题,也就是将error延迟到发射完毕所有的项目之后。

concatMapEager:eager是急切的意思,急切连接意味着一旦订阅者订阅,该操作符就订阅所有源发布者。

concatMapEagerDelayError:延迟处理error

concatMapIterable:注意上面测试用例,我们可以看到,代码中将每个数字后面都连接了一个“*”,然后添加进集合里,在观察者回调accept中接收到的便是这样一个增加“*”后的项目,也就正好印证了接口说明:返回一个Flowable,它将源Publisher发出的每个项目与Iterable中与选择器生成的项目对应的值相连接。

总之以上都是通过自定的函数将发射某种类型项目的Flowable转换成了发射另外一个种类型项目的Flowable(上面测试将Integer转换成String在发射)

猜你喜欢

转载自blog.csdn.net/weixin_36709064/article/details/82946069
今日推荐