RxJava from source code to application - a brief introduction and review of RxJava (3-1)

1. Introduction to RxJava

This sentence is from the official website

Baidu Translate asynchronous - http://fanyi.baidu.com/translate#en/zh/asynchronous

Baidu Translate event-based —— http://fanyi.baidu.com/translate#en/zh /event-based

1、A library for composing asynchronous and event-based programs by using observable sequences

Asychronous

1. Asynchronous, RxJava is an asynchronous library

2. Callback based

Event-based

1. Event-based

2. A library for event distribution, a library for message passing

Two-1, RxJava1 code example

Let's first look at the example of RxJava1, and then look at the example of RxJava2.

First we use the create() method of Observable to create an Observable object

The parameter stored in this create method is an OnSubscribe

Finally, subscribe() an Observer through the created Observable object

There are three methods in Observer, namely OnComplete(), onError(), onNext()

There is a Call() method in this OnSubscribe, and the parameter inside is a subscribe

Then calling the subscribe method of Observable returns a Subscription.

This is an example of the simplest RxJava1.

 
 
findViewById(R.id.btnRxJava1).setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) {    

        Subscription tSubscription = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
public void call(Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()) {            
                    subscriber.onNext("test");
                    subscriber.onCompleted();
                }
            }
        }).subscribe(new Observer<String>() {
            @Override
public void onCompleted() {            
                System.out.println("onCompleted");
            }

            @Override
public void onError(Throwable e) {            

            }

            @Override
public void onNext(String s) {            
                System.out.println("onNext:" + s);
            }
        });
    }
});

During the running process, first call the onNext() method through subscribe to pass a string, and then when calling OnComplete(), print the string OnCompleted, and when calling OnNext(), in addition to printing the method name of onNext, it also prints the string of OnCompleted. Then print out the test passed down above.

It will first print the onNext method name, then print the passed string and then onCompleted(). This is a relatively simple example of RxJava1.

Two-2, the basic elements of RxJava1

1、Observable

2、Observer

3、Subscription

4、OnSubscribe

5、Subscriber

Three-1, RxJava2 code example

Annotate the RxJava1 code first, then switch the library

    //    compile 'io.reactivex:rxjava:1.3.0'
//    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.0'

Then we look at the code of RxJava2

 findViewById(R.id.btnRxJava2).setOnClickListener(new View.OnClickListener() {
        @Override
public void onClick(View view) {        
            Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    if (!e.isDisposed()) {
                        e.onNext("1");
                        e.onNext("2");
                        e.onNext("3");
                        e.onNext("4");
                        e.onNext("5");
                        e.onNext("6");
                        e.onNext("7");
                        e.onNext("8");
                        e.onNext("9");
                        e.onNext("10");
                        e.onComplete();
                    }
                }
            }).subscribe(new Observer<String>() {
                @Override
                public void onSubscribe(Disposable d) {
                    System.out.println("onSubscribe");
                }

                @Override
                public void onNext(String value) {
                    System.out.println("onNext:" + value);
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {
                    System.out.println("onCompleted");
                }
            });

            Flowable.create(new FlowableOnSubscribe<String>() {
                @Override
                public void subscribe(FlowableEmitter<String> e) throws Exception {
                    if (!e.isCancelled()) {
                        e.onNext("test");
                        e.onComplete();
                    }
                }
            }, BackpressureStrategy.DROP).subscribe(new Subscriber<String>() {
                @Override
                public void onSubscribe(Subscription s) {
//                        s.request(Long.MAX_VALUE);
                    System.out.println("onSubscribe");
} @Override public void onNext(String s) { System. out.println( "onNext:" + s); } @Overridepublic void onError(Throwable t) { } @Overridepublic void onComplete() { System. out.println( "onCompleted"); } }); } });}

You can see that a corresponding Observable is created, which is something we are familiar with and have just seen.

There is also a Flowable below. What is the difference between the two? Let’s briefly explain it first, because when using RxJava, there will be a back pressure problem , then in RxJava, there are some Observables that can handle back pressure problems. Yes, some can't handle the back pressure problem, so now, in RxJava2, they split the two, so now Observable doesn't deal with the back pressure problem at all, and a new Flowable will be split. There is a back pressure strategy, which is the difference between the two. Let's first take a look at how this Observable that is not processed at all is used:

首先还是create()的方法,传进去一个ObservableOnSubscribe,然后这里边有一个subscribe()方法,这里有一个新面孔Emitter,之后就是熟悉的调用subscribe()方法,里边传进去一个Observer,可以看到里边多了一个方法OnSubscribe(),里边有一个比较陌生的参数Disponsable,之后又是三个熟悉的方法onNext(),onError(),onCompleted()。

接下来我们看一下新拆出来这个解决背压问题的Flowable是怎么写的:

其实还是同一个模式的有一个create()方法,里边传进去一个FlowableOnSubscribe,之后里边是一个subscribe()方法,参数是一个Emitter,这里有一个第二个参数就是背压策略,这里我们先传入一个Drop丢弃,之后还是熟悉的Subscribe(),参数是Subscriber,这里也是多了一个方法,onSubscribe()的时候,里边会有一个Subscription,之后又是三个熟悉的onNext(),onError(),onComplete(),运行一下。

首先先说上边的Observable,首先是onSubscribe,之后是发送数据test,第三个是onCompleted,上边这个没有问题跟我们之前看得没有什么太大区别,我们接下来看下边这个,首先OnSubscribe是没有问题的,但是接下来的按照前边说的它应该打印onNext,但是它并没有打,而是直接打了OnCompleted,那么这是为什么呢,因为解决背压问题要这样写,要有一个响应式拉取的一个策略,

@Override
public void onSubscribe(Subscription s) {
    s.request(Long.MAX_VALUE);
    System.out.println("onSubscribe");
}

我们再跑一下

现在可以看到这个onNext打印出来了,因为这个Flowable是专门为处理背压来做的,所以在OnSubscribe()的时候需要手动的request一下数据做一个响应式的拉取。

三-2、RxJava2基本元素

1、Observable和Flowable

2、Observer和Subscriber

3、Disponsable和Subscription

4、相应的OnSubscribe

5、Emitter

下章分析

1、RxJava1的基本元素

Guess you like

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