preface

  • Official definition:RxJavaIs a Java VM implementation of reactive extensions: a library for composing asynchronous and event-based programs using observable sequences
  • Features:RxjavaBecause of its chain call based on event flow, simple logic & simple to use, it is welcomed by major Android developers.

function

  • Asynchronous operation based on event flow is equivalent to The functions of AsyncTask and Handler in Android

The characteristics of

  • Chain calls based on event flow
  • Logic is concise
  • To achieve elegant
  • Using a simple
  • As the complexity of your program logic increases, you can still keep your code simple

The principle of

  • RxjavaThe principle is based on an extended observer pattern

Observables send events sequentially to observers by subscribing. The observers receive events sequentially and respond accordingly


  • An Observable generates events
  • An Observer receives an event and gives a response
  • Subscribe connects the observed to the observer
  • Events are a bridge between observers and observers

The basic use

1. Traditional way 1

  • Join the rely on
   implementation 'the IO. Reactivex. Rxjava2: rxjava: 2.2.8'
    implementation 'the IO. Reactivex. Rxjava2: rxandroid: 2.1.1'
Copy the code
  • Creates an observed Observable & generates events
Observable<Integer> Observable = Observable. Create (new ObservableOnSubscribe<Integer>() {// 1 2. @override public void subscribe(ObservableEmitter<Integer> Emitter) throws Exception {// Pass ObservableEmitter class object generates events and notifies the observer. OnNext (1); Emitters. OnNext (1); Emitters. emitter.onNext(2); emitter.onNext(3); emitter.onComplete(); // Event complete, you can choose to continue to send events}});Copy the code
  • Create observers and define the behavior of responding to events
Observer<Integer> Observer = new Observer<Integer>() {// Override public void onSubscribe(Disposable)  d) { Log.d(TAG,"Start connecting"); Override public void onNext(Integer value) {log.d (TAG, TAG) {subscribe () @override public void onNext(Integer value) {Log."Handling events"+ value   );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "Handling events"+ value   );
            }
            }

            @Override
            public void onComplete() {
               Log.d(TAG, "Event completed. No more events received."); }}};Copy the code
  • The observer is connected to the observed by subscribing
   observable.subscribe(observer);
Copy the code

2. Based on event flow chain mode

Observable.create(new ObservableOnSubscribe<Integer>() { // 1. @override Public void subscribe(ObservableEmitter<Integer> Emitter) throws Exception {emitter. OnNext (1); emitter.onNext(2); emitter.onNext(3); emitter.onComplete(); } }).subscribe(new Observer<Integer>() { // 2. By connecting the observer to the observed via subscribe // 3. Override public void onSubscribe(Disposable d) {Log."Start using subscribe connections."); Override public void onNext(Integer value) {log.d (TAG, TAG) {subscribe () @override public void onNext(Integer value) {Log."Handling events"+ value   );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "Handle Error events, no longer receive events");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "Process Complete event, no longer receive event"); }}); }Copy the code
  • The observer cannot continue to receive events from the observed, but the observed can continue to send events
Observer<Integer> Observer = new Observer<Integer>() {// 1. Private Disposable mDisposable; @Override public void onSubscribe(Disposable d) { Log.d(TAG,"Start using subscribe connections."); // 2. Assign mDisposable = d to Disposable; } @Override public void onNext(Integer value) { Log.d(TAG,"To the Next event"+ value +"Respond"  );
                if(value == 2) {// Dispose () disconnects the observer and the observed after receiving the second event. Log.d(TAG,"The connection has been broken:" + mDisposable.isDisposed());
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "Handling Error Events"); } @override public void Override public voidonComplete() {
                Log.d(TAG, "Handle the Complete Event"); // Cannot receive the Complete event}};Copy the code

conclusion

  • RxjavaThe principle is based on an extended observer pattern

Observables send events sequentially to observers by subscribing. The observers receive events sequentially and respond accordingly


  • An Observable generates events
  • An Observer receives an event and gives a response
  • Subscribe connects the observed to the observer
  • Events are a bridge between observers and observers

This completes the basic introduction and use of Rxjava. Thank you for reading

Reference article:

Android Rxjava: This is a clear and easy to understand introduction to Rxjava

Welcome to darryrzhong, more dry goods waiting for you to get yo.

A little red heart, please! Because your encouragement is the biggest power that I write!

Please pay attention to more wonderful articles

  • Personal blog: Darryrzhong
  • The Denver nuggets
  • Jane’s book
  • SegmentFault
  • Moocs Notebook