RxJava2 notes (5) - operator. flatMap()

Please indicate the source of Reprint: https://blog.csdn.net/user11223344abc

Reference material

flatMap

Official words:

FlatMap — transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable

  • transform the items emitted by an Observable into Observables.
    Convert the Observable of a launched item to Observables.

  • then flatten the emissions from those into a single Observable.
    These emissions are then turned into a single Observable. (the word "discharge" reminds me of water pipe greatly >_

Analysis:

Both map and FlatMap belong to the operators of transformation. The so-called transformation is to change the shape of the transmitter in the process of transmitting - receiving, and change the shape of the transmitter in the initial receiving. Map is relatively simple, such as:

 Observable<String> mapObservable = retrofitPostApi.getPathDoubadataRx(url).map(new Function<ResponseData, String>() {

            @Override
            public String apply(ResponseData responseData) throws Exception {
                return responseData.toString();
            }
        });

You can see that the return value of the apply method is String, which represents the abnormal target shape.

Let's look at the application method of flatMap. It is an ObservableSource. After being wrapped by the ObservableSource class, there are many things you can do.

    .flatMap(new Function<Integer, ObservableSource<String>>() {
                        @Override
                        public ObservableSource<String> apply(Integer integer) throws Exception {
                        Log.e(TAG, TAG + "__apply");
                        final List<String> list = new ArrayList<>();
                        list.add("I am value " + integer);
                        return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
        }
    })

Example

A normal example:

    Observable.create(new ObservableOnSubscribe<Integer>() {
                        @Override
                        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                            emitter.onNext(1);
                            emitter.onNext(2);
                            emitter.onNext(3);
                        }
                    }).flatMap(new Function<Integer, ObservableSource<String>>() {

                        @Override
                            public ObservableSource<String> apply(Integer integer) throws Exception {
                            Log.e(TAG, TAG + "__apply");
                            final List<String> list = new ArrayList<>();
                            list.add("I am value " + integer);
                            return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
                        }

                    }).subscribe(new Consumer<String>() {
                        @Override
                        public void accept(String s) throws Exception {
                            Log.e(TAG, s);
                        }
                    });

Output is:

05-04 15:59:40.977 6680-7337/zj.playretrofit E/HardPlayReTro: I am value 1
05-04 15:59:40.979 6680-7338/zj.playretrofit E/HardPlayReTro: I am value 2
05-04 15:59:40.983 6680-7339/zj.playretrofit E/HardPlayReTro: I am value 3

A less normal example

        Observable.create(new ObservableOnSubscribe<Integer>() {
                        @Override
                        public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                            emitter.onNext(1);
                            emitter.onNext(2);
                            emitter.onNext(3);
                        }
                    }).flatMap(new Function<Integer, ObservableSource<String>>() {

                        @Override
                            public ObservableSource<String> apply(Integer integer) throws Exception {
                            Log.e(TAG, TAG + "__apply");
                            final List<String> list = new ArrayList<>();
                            for (int i = 0; i < 4 ; i++) {
                                list.add("I am value " + integer);
                            }
                            return Observable.fromIterable(list).delay(10, TimeUnit.MILLISECONDS);
                        }

                    }).subscribe(new Consumer<String>() {
                        @Override
                        public void accept(String s) throws Exception {
                            Log.e(TAG, s);
                        }
                    });

Output is:

05-04 16:30:27.258 15861-16394/zj.playretrofit E/HardPlayReTro:
    I am value 1
    I am value 1
05-04 16:30:27.259 15861-16394/zj.playretrofit E/HardPlayReTro:
    I am value 1
    I am value 1
05-04 16:30:27.259 15861-16153/zj.playretrofit E/HardPlayReTro: 
    I am value 2
    I am value 2
    I am value 2
05-04 16:30:27.260 15861-16154/zj.playretrofit E/HardPlayReTro:
    I am value 3
    I am value 3
    I am value 3
    I am value 3
05-04 16:30:27.260 15861-16153/zj.playretrofit E/HardPlayReTro: 
    I am value 2

It can be seen that flatMap not only changes the type of transmission source data from one dimension, but also changes the carrier (which can be understood as pipeline, channel and water pipe) of transmission source data, and it is also disordered.

Tags: less

Posted on Fri, 20 Mar 2020 13:08:06 -0400 by schoenung