Android Rxjava operator Detail series: transform operator

Source: Internet
Author: User

Rxjava, because of its chain call based on event flow, logic simple & easy to use features, by the major Android developers welcome.
Github

If you do not know RxJava, see the article: Android: This is a clear & understandable RxJava Getting Started tutorial

The reason why Rxjava is so popular is that it offers a rich & powerful operator that can do almost all the functional requirements
Today, I will give you a detailed introduction to the Rxjava operator of the most commonly used transform operators, and with Retrofit combined with Rxjava demo teaching, I hope you will like.
This series of articles is based mainly on Rxjava 2.0
The next time, I will continue to launch a series of articles on Android Rxjava 2.0, including principles, operators, application scenarios, back pressure, etc., interested in continuing to focus on Carson_ho's Android development notes!!


Directory

1. Role

Process (i.e. transform) the events/entire sequence of events in an event sequence, transforming them into different events/entire sequence of events
The specific principle is as follows

2. Type

The following transformation operators are common in Rxjava:

I'll cover each of these operators in more detail below.

Note: This article only explains the transformation operators commonly used by RXJAVA2 in the development process.
3. Application scenario & corresponding operator introduction

Next, I'll explain the transformation operators in RXJAVA2
Note: Before using the Rxjava 2 operator, remember to add dependencies in the project's Gradle:
dependencies {
Compile ' io.reactivex.rxjava2:rxandroid:2.0.1 '
Compile ' io.reactivex.rxjava2:rxjava:2.0.7 '
Note: RXJAVA2 and RXJAVA1 cannot coexist, that is, dependencies cannot exist simultaneously
}
1
2
3
4
5
3.1 Map ()

Role
Each of the 1 events sent to the observer is processed by the specified function, transforming it into another event

That is, the event sent by the Observer is converted to any type event.
Principle

Application Scenarios
Data type conversions

Specific use
The following is an example of using map () to transform an event's arguments from an integer to a string type.

Using Rxjava-based event flow-linked operations
Observable.create (New observableonsubscribe<integer> () {

1. The Observer sends the event = parameter = Integer = 1, 2, 3
@Override
public void Subscribe (observableemitter<integer> emitter) throws Exception {
Emitter.onnext (1);
Emitter.onnext (2);
Emitter.onnext (3);

}
2. Use function functions in the map transform operator to uniformly transform events sent by the Observer: integer transforms into string types
}). map (new Function<integer, string> () {
@Override
Public String apply (integer integer) throws Exception {
Return "uses the map transform operator to transform the parameters of event" + Integer + "from integer" +integer + "to String type" + integer;
}
}). Subscribe (New consumer<string> () {

3. When an observer receives an event, it receives the transformed event = string type
@Override
public void Accept (String s) throws Exception {
LOG.D (TAG, s);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
Test results

As can be seen from the above, map () converts an Integer type object in a parameter to a string object and returns

At the same time, the parameter type of the event is also changed from the Integer type to the String type
3.2 FlatMap ()

Function: The sequence of events sent by the Observer is split & individually converted, merged into a new sequence of events, and finally sent

Principle

Creates a Observable object for each event in the sequence of events;
The new events that are converted to each original event are put into the corresponding observable object;
Merges each newly created observable into a new, total observable object;
The new, total observable object sends the newly merged sequence of events to the Observer (Observer)

Application Scenarios
To transform the entire sequence of events sent by the Observer.

Specific use

Using Rxjava-based event flow-linked operations
Observable.create (New observableonsubscribe<integer> () {
@Override
public void Subscribe (observableemitter<integer> emitter) throws Exception {
Emitter.onnext (1);
Emitter.onnext (2);
Emitter.onnext (3);
}

Using the Flatmap () transform operator
}). FlatMap (New Function<integer, observablesource<string>> () {
@Override
Public observablesource<string> apply (integer integer) throws Exception {
Final list<string> List = new arraylist<> ();
for (int i = 0; i < 3; i++) {
List.add ("I am the event" + Integer + "Split sub-event" + i);
By splitting the sequence of events produced by the Observer in Flatmap, and then converting each event to a new send three string event
Final merge, then sent to the Observer
}
return observable.fromiterable (list);
}
}). Subscribe (New consumer<string> () {
@Override
public void Accept (String s) throws Exception {
LOG.D (TAG, s);
}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
Test results

Note: The sequence of event sequences generated by the new merge is unordered, regardless of the order in which the events were sent by the old sequence
3.3 Concatmap ()

Function: Similar to the Flatmap () operator
The difference from Flatmap () is that the order of the sequence of events generated by the split & re-merge = the order in which the old sequence is produced by the Observer

Principle

Application Scenarios
An orderly transformation of the entire sequence of events sent by the Observer

Specific use

Using Rxjava-based event flow-linked operations
Observable.create (New observableonsubscribe<integer> (www.078881.cn/) {
@Override
public void Subscribe (observableemitter<integer> emitter) throws Exception {
Emitter.onnext (1feifanshifan8.cn/);
Emitter.onnext (2);
Emitter.onnext (3);
}

Using the Concatmap () transform operator
}). Concatmap (New Function<integer, observablesource<string>> () {
@Override
Public observablesource<string> apply (integer integer) throws Exception {
Final list<string> List = new arraylist<> ();
for (int i = 0; i < 3; i++) {
List.add ("I am the event" + Integer + "Split sub-event" + i);
By splitting the sequence of events produced by the Observer in Concatmap, and then converting each event to a new send three string event
Final merge, then sent to the Observer
}
return observable.fromiterable (list);
}
}). Subscribe (new consumer<string> (www.jcx127.cn) {
@Override
public void Accept (String www.tianhengyl1.com s) throws Exception {
LOG.D (TAG, s);
}

Test results

Note: The sequence of event sequences generated by the new merge is ordered, that is, the order in which events are sent strictly according to the old sequence
3.4 Buffer ()

Role
Periodically get a certain number of events from the observers (obervable) to be sent & put them in the buffer, and send them eventually

Android Rxjava operator Detail series: transform operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.