preface

  • RxjavaBecause of itsChain call based on event flow, simple logic & easy to useThe characteristics of the deepAndroidDeveloper welcome.

If you are not familiar with RxJava, check out Android: a clear and easy-to-understand introduction to RxJava

  • RxJavaThat’s why it’s so popularProvides rich & powerful operators that can fulfill almost all functional requirements
  • Today, I’m going to present one to youRxJavaOperator detailed use of walkthrough, I hope you will like.
  1. This series of articles is based onRxjava 2.0
  2. 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.

  • RxJavaIt 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

  1. This example will combineRetrofitRxJavaTo explain
  2. 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

  1. This example will combineRetrofitRxJavaTo explain
  2. Network request nested callback (RxJava

3.3 Combine/merge operators

  • Combines multiple Observables & merges events that need to be sent

  • Application scenarios

    1. Combine multiple observers
    2. Merging multiple events
    3. Append sending events before sending events
    4. 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

  1. Merge data sources
  2. Get cache data from disk/memory cache
  3. 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

  1. Connect (subscribe) observer & observed
  2. Thread scheduling (switching)
  3. Error handling
  4. Event lifecycle operations
  5. Delay operation
  6. 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

  1. Android RxJava: Thread control (switching/scheduling)
  2. Network request error reconnection (Retrofit)
  3. Android RxJava :(conditional) network request polling

3.5 Filter operators

  • Function filters/filters events sent by observables and events received by observers

  • Application scenarios

    1. Filter events based on specified conditions
    2. Filter events based on the specified number of events
    3. Filter events based on the specified time
    4. 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

  1. Android RxJava application: Lenovo search optimization
  2. 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 onRxJava2All 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!!


Please give vertex a thumbs up! Because your encouragement is the biggest power that I write!


Welcome to follow Carson_ho on wechat