preface
Rxjava
Because of itsChain call based on event flow, simple logic & easy to useThe characteristics of the deepAndroid
Developer welcome.
If you are not familiar with RxJava, check out Android: a clear and easy-to-understand introduction to RxJava
RxJava
That’s why it’s so popularProvides rich & powerful operators that can fulfill almost all functional requirements- Today, I’m going to present one to you
RxJava
Operator detailed use of walkthrough, I hope you will like.
- This series of articles is based on
Rxjava 2.0
- In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!
All code demos in this article are stored at Carson_Ho’s Github address
directory
1. Introduction
The RxJava operators are described as follows:
Type 2.
RxJava
It is very powerful, so the corresponding operator types are very large- RxJava operators are classified as follows:
- I’ll look at the operators for each class in detail below
3. Operator details
Note: Before using the RxJava 2 operator, remember to add dependencies to your project Gradle:
dependencies {
compile 'the IO. Reactivex. Rxjava2: rxandroid: 2.0.1'
compile 'the IO. Reactivex. Rxjava2: rxjava: 2.0.7'// Note: RxJava2 and RxJava1 cannot coexist, i.e. dependencies cannot exist at the same time}Copy the code
3.1 Creating operators
-
Creates an Observable object & sends an event.
-
Application scenarios
- Type Based on the above application scenarios, the types of operators to be created include:
-
Operators & Application scenarios Android RxJava: the most basic operator description – create operator
-
Below, I’ll show you a common practical requirement example for creating an operator: network request polling
- This example will combine
Retrofit
和RxJava
To explain- Android RxJava: (unconditional) network request polling
3.2 Transform operators
- Function To process (transform) the events/whole sequence of events into different events/whole sequence of events
- Specific principles
-
Application Scenario Nested Callback hell
-
Type According to the above application scenarios, the common types of transformation operators include:
-
Android RxJava: Graphic description of the conversion operator
-
In the following sections, I’ll show you a common example of a practical requirement for transformation operators: network request nested callbacks
- This example will combine
Retrofit
和RxJava
To explain- Network request nested callback (RxJava
3.3 Combine/merge operators
-
Combines multiple Observables & merges events that need to be sent
-
Application scenarios
- Combine multiple observers
- Merging multiple events
- Append sending events before sending events
- Statistics the number of sent events
-
Type Based on the above application scenarios, the common combination/merge operators are:
-
Android RxJava: Combine/merge operators tutorial
-
In the following sections, I’ll walk you through common practical requirements cases for composite operators
This example will be illustrated in combination with Retrofit and RxJava
- Merge data sources
- Get cache data from disk/memory cache
- Android RxJava practical application explanation: joint judgment
3.4 Functional operators
-
Functions help an Observable fulfill some functional requirements when sending events
-
Actual Application Scenarios
- Connect (subscribe) observer & observed
- Thread scheduling (switching)
- Error handling
- Event lifecycle operations
- Delay operation
- Repeat send operation
-
Types According to the above application scenarios, the common functional operators are:
-
Android RxJava: functional operators are fully explained
-
In the following sections, I’ll walk you through common practical requirements cases for composite operators
This example will be illustrated in combination with Retrofit and RxJava
- Android RxJava: Thread control (switching/scheduling)
- Network request error reconnection (Retrofit)
- Android RxJava :(conditional) network request polling
3.5 Filter operators
-
Function filters/filters events sent by observables and events received by observers
-
Application scenarios
- Filter events based on specified conditions
- Filter events based on the specified number of events
- Filter events based on the specified time
- Filter events based on the specified event location
-
Type According to the preceding application scenarios, the common filtering operators are as follows:
-
Operators & Application Scenarios Android RxJava: Filter operators are explained in detail
-
In the following sections, I’ll walk you through common practical requirements cases for composite operators
This example will be illustrated in combination with Retrofit and RxJava
- Android RxJava application: Lenovo search optimization
- Android RxJava practical application explain: Android RxJava practical application explain: function of anti-shake
3.6 Conditional/Boolean operators
-
Function determines whether an event sent by an Observable meets conditions by setting the function
-
In type RxJava2, the types of conditional/Boolean operators include:
- Android RxJava: conditional/Boolean operators
This concludes the explanation of operators in RxJava 2.
4. To summarize
- This paper mainly focuses on
RxJava2
All of the commonly used type operators in the
- In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!