(15) Learn RxJava Readme

Foreword: I have recently started to look at the recruitment requirements of other companies. After three years of work, I can’t always do repetitive work. Then I see that most of the recruitment requirements will mention some technologies that I have never heard of, such as RxJava. I will learn it now when I have time. .


Reference blog: Click to open the link

(This article focuses on the principle of code combination, I haven't read it yet, I like to do it while watching it)


RxJava Readme address: Click to open the link


demo: Click to open the link

Failed demo: click to open the link

1. A brief introduction to RxJava

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.

The above is the introduction to RxJava in the readme. Use Google to translate and adjust the order:

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

It extends the Observer pattern to support sequences of data/events, and adds operators that allow you to step away from things like low-level threading, synchronization, thread safety, and concurrent data structures, to declaratively compose sequences instead .


Personal understanding:

1. Extended the observer pattern

2. It encapsulates threads and concurrency that ordinary people can't play, and it is easy to use foolishly


2. Practice based on Readme

Just looked at it, it's a lot, let's go step by step.

2.1 helloworld

To get started, always ask for hello world. See the guide below, which is "Create a new java project" (the Java project will report an error, but Android will do), add a dependency, and then write a helloworld program, pay attention to distinguish between 1.8 and before Version.

Results of the:

The expected helloworld did not appear, but an error was reported. It is obvious that the package can be imported, so why the execution will go wrong.

Well, I created a java project and it doesn't seem to be able to run. I also created an Android project and it can run normally. Is rxjava said to be java but it is actually for Android?


You can print out hello world normally, so let's do this first.


2.2 Basic classes

I used the Flowable class just now, there is a link in the picture above, click on it and take a look.

In short, it describes the processing flow: the observer and the observed, the observed submits a series of tasks, and the observer processes it. Similar to buttons (observed) and listeners (observers), each click will cause the listener to call the onClick method.


2.3 Terminology

Wow, I always feel that this RxJava is unacceptable. It is estimated that it will be more comfortable to read this Readme after learning a few terms and understanding its meaning. Suddenly I feel a lot, so I make the catalogs first, and then look at them one by one. . .


2.3.1 Upstream and downstream

According to the following explanation, it is a series of steps, looking up is Upstream, looking down is Downstream



2.3.2 Objects in motion

In the RxJava documentation, emission, emits, item, event, signal, data and message are considered synonyms and represent objects that travel along the data flow.


2.3.3 BackPressure


When a data flow runs through asynchronous steps, each step may perform different things at different speeds. To avoid overwhelming these steps, which often manifest as increased memory usage due to temporary buffering or the need to skip/drop data, so called backpressure is applied, which is a form of flow control where steps can indicate that they are ready to How many items to handle . This allows to limit the memory usage of the data stream where there is usually no way of knowing how many items the upstream will take to send it to.

In RxJava, the dedicated Flowable class specifies support for backpressure , and Observable is dedicated to non- backpressure operations (short sequences, GUI interactions, etc.). The other types, Single, Maybe and Completable do not support backpressure and should not support backpressure ; there is always room to temporarily store an item.

2.3.4 Assembly time


By applying various intermediate operators to prepare the data flow, these operations are performed at the so-called assembly time. (Assembly time or preparation time?)

At this point, the data does not start to flow and no side effects occur.


2.3.5  Subscription time


Subscription time: The temporary state after calling subscribe() after the processing steps are established inside the data stream.

This is when a subscription side effect is fired (see doOnSubscribe). Some sources stop or start firing items immediately in this state.


2.3.6 Runtime

This is the state when the stream is signaling items, errors, or completions:

In fact, this is the case when the body of the example given above executes. It feels like the Runtime of Android's RuntimeException.


2.3.7 Simple background  computation

The logic of background calculation and foreground display is presented in a form similar to the builder in the dialog box, and it feels like the code of FutureTask is simplified.

2.3.8 Schedulers

Schedulers function similar to Executors and can be used for different types of work.


2.3.9  Concurrency within a flow


I don’t feel that I understand it very well. It is estimated that I have to understand the function of the API and look back. It will be much better.


2.3.10 Parallel processing



2.3.11 Dependend sub-flows



2.3.12 Continuations


2.3.13 Type conversions


2.3.14 Operator naming conventions



3. Version and Notes


to be continued. . .




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852538&siteId=291194637