RxJava operators - transform operators

Posted by Millar on Thu, 30 Apr 2020 22:53:26 +0200

The event / whole event sequence in the event sequence is processed (i.e. transformed) to different event / whole event sequence

map

Each event sent by the observed is processed by a specified function, so that it can be transformed into another event
That is, the event sent by the observer is converted to any type of event.

Application scenario
Data type conversion

Chestnut:

        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                subscriber.onNext(1);
                subscriber.onNext(2);
                subscriber.onNext(3);

            }
        }).map(new Func1<Integer, String>() {
            @Override
            public String call(Integer integer) {
                return "Use Map Transform operator will event" + integer + "Parameter from integer" + integer + " Convert to string type" + integer;
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                Log.d(":..nht", s);
            }
        });

12-20 10:26:59.652 11717-11717/com.sankuai.moviepro D/:..nht: Use MapTransform operator will event1Parameter from integer1 Convert to string type1
12-20 10:26:59.652 11717-11717/com.sankuai.moviepro D/:..nht: Use MapTransform operator will event2Parameter from integer2 Convert to string type2
12-20 10:26:59.652 11717-11717/com.sankuai.moviepro D/:..nht: Use MapTransform operator will event3Parameter from integer3 Convert to string type3

flatmap

Function: split the event sequence sent by the observer & transform it separately, merge it into a new event sequence, and send it at last

Application scenario
Unordered transformation of the entire sequence of events sent by the observer

??? What's the difference with map??
I still don't understand
http://www.jianshu.com/p/c820afafd94b
map and flatMap in RxJava
http://blog.csdn.net/new_abc/article/details/48025513

concatmap

If flatmap understands it, then concatmap is easy to say.

buffer

Get a certain number of events from the events that the observable needs to send on a regular basis & put them in the cache and send them finally

Application scenario
Caching events sent by observers

Chestnut:

        Observable.just(1,2,3,4)
                .buffer(3, 1)
                .subscribe(new Observer<List<Integer>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<Integer> integers) {
                        for (int i = 0; i < integers.size(); i++) {
                            Log.d(":..nht", integers.get(i) + "");
                        }
                    }
                });

12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 1
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 2
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 3
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 2
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 3
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 4
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 3
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 4
12-20 20:20:51.054 6491-6491/com.sankuai.moviepro D/:..nht: 4

Please refer to the figure for understanding:

reference material

Android RxJava: text detailed transformation operator
https://www.jianshu.com/p/904c14d253ba

Topics: Android