Let’s learn more about RxJava

Same rules, continue to use Kotlin to learn, Java can also understand, try to write easy to understand, if the article has mistakes, welcome comments correction

First let’s try to learn some basic (?)

1. Prepare knowledge

1.1 the asynchronous

Let’s start with asynchrony, because RxJava on Github introduces itself: a library for composing asynchronous and event-based programs by using observable sequences. A library that uses observable sequences to compose asynchronous, event-based programs on the JVM; As we can see, RxJava certainly has a deep connection with asynchrony. So what is asynchrony?

Asynchrony is a statement or behavior in which a caller can continue execution after making an invocation request without waiting for the result of the invocation to return.

To put it simply (?) Synchronous: Mother feed me to eat, mother feed me to eat a mouthful, I can eat a mouthful, but when I eat my mother does not give me food, I have to swallow the mother gave me asynchronous: Dad hello I to have a meal, father scooped out one mouthful, the food on the plate, I used to eat, I am in the process of eating, dad continued to scoop out the food on the plate, dad don’t scoop back when I finish, but constantly spoon, spoon out after he ran to play, I have to do is to eat a bite plate, of course, dad this time can also keep the rice scoop into the elder sister, That is, you can do the other act after you ladle my meal, instead of waiting for me to finish it

1.2 Observer Mode

In RxJava2, we can implement the observer mode in two ways: (1) Back pressure is not supported: (2) back pressure is not supported: Observable & Observer (2) Support back pressure: Flowable & SubScriber

1.3 back pressure

In the introduction of the asynchronous, we see dad kept scooped out the meal, and assuming that the father having dinner soon, I eat slowly, like this dish will be more and more, if the plate is not big enough, then there will be a problem, that’s father will be because it was full swing not bottom plate of rice and the rice scoop into the trash can, so we will lost a meal, or even a lot of rice, This is a serious mismatch between event sending and receiving, resulting in loss or even OOM. What is the solution to this problem? In RxJava1.0, the sender sends how much, the receiver receives, and in RxJava2, the use of Flowable, that is, response pull, feedback control, the use of back pressure strategy mode, we simply feed dad, and then I just after, look back to tell dad how much I can next, Dad knew I was just the speed of the rice after controlling the amount of rice and speed, let me just comfortable, but also to avoid the risk of full plate. As for the implementation of this, I’ll talk about it in the next section.

2, the characteristics of

The main features that attracted me to RxJava were: – Chain calls based on event flow – logic simplicity – simplicity of use – it keeps the program logic simple as it gets more complex.

3. Basic use

3.1 Basic Steps

(1) Create the observed and generate the event; (2) create the observer and define its actions on the event; (3) connect the observed and the observer by subscribing

3.2 Basic Methods

(1)onNext(t: String) : send event (2)onError(e: (3)onComplete() : (4)onSubscribe(D: Disposable) : connect, subscribe

3.3 example

We can use Kotlin to write the following simple example

/ / the upstream observed val mObservable = IO. Reactivex. Observables. Create (ObservableOnSubscribe < String > {it. OnNext (" 1 ") it. OnNext (" 2 ") It.onnext ("3") it.onComplete() // When the upstream sends an onComplete, events after the upstream onComplete will continue to be sent, When the upstream sends an onError, events after the upstream onError continue to be sent. OnComplete and onError must be unique and mutually exclusive, that is, no more oncompletions, no more onErrors, and no one onComplete first. And then an onError, and vice versa, It works fine to send multiple onCompletions, but if you send multiple onCompletions, }) // Observer val mObserver = object: Observer<String>{ override fun onComplete() { Log.d("onComplete", "88") } override fun onError(e: Throwable) { } override fun onNext(t: String) { Log.d("onNext", "val:$t") } override fun onSubscribe(d: Compounddisposable) {log. d("onSubscribe", "hi")}}Copy the code

Of course, we can also use the simple Observable notation to keep our code simple

val mObservable = Observable.create<String> {
            it.onNext("1")
            it.onNext("2")
            it.onNext("3")
            it.onComplete()
        }
Copy the code

4, summarize

It’s worth noting that IN this article, I only use CREat to create an Observable. However, there are many ways to create an Observable in RxJava, including creat, just, etc. You can check out the big guy’s article RxJava2 operator summary – everything you want to know is here. Next dig a hole, plan to write a small project, use RxJava and other methods, as soon as possible

5. Reference documents

Android RxJava: this is a clear and easy to understand RxJava introduction to the RxJava2 operator summary — you want all in here

(Their writing is really better than mine.)