Basic concept and implementation of RxJava

Posted by rage2021 on Fri, 22 Oct 2021 14:44:01 +0200


RxJava is a library that uses observable sequences to form asynchronous, event based programs on the Java VM. In a word, asynchronous.
It is an extended observer model.

Four basic concepts

  • Observable: observable
  • Observer: observer
  • subscribe: subscribe
  • event

Callback method

  • onNext(): equivalent to onClick() / onEvent()
  • onCompleted(): called when no new onNext() method is triggered
  • onError(): the time queue is abnormal, and the queue is automatically terminated, and only one of them can be triggered by the and onCompleted() methods.

Basic implementation

Observer observer

Observer<String> observer = new Observer<String>() { 
    @Override 
    public void onNext(String s) { Log.d(tag, "Item: " + s); } 
    @Override 
    public void onCompleted() { Log.d(tag, "Completed!"); } 
    @Override 
    public void onError(Throwable e) { Log.d(tag, "Error!"); }
};

Subscriber subscriber

Subscriber extends the Observer interface, but their basic usage is exactly the same

Subscriber<String> subscriber = new Subscriber<String>() { 
    @Override 
    public void onNext(String s) { Log.d(tag, "Item: " + s); } 
    @Override 
    public void onCompleted() { Log.d(tag, "Completed!"); } 
    @Override 
    public void onError(Throwable e) { Log.d(tag, "Error!"); }
};

Additional methods:

  • onStart(): This is a new method added by the Subscriber. It will be called just before the Subscriber starts and the event has not been sent. It can be used to do some preparatory work. If there is a thread requirement, it is not applicable because the thread that occurs in the subscribe is always called.
  • unsubscribe(): used to unsubscribe. After this method is called, the Subscriber will no longer receive events; isUnsubscribed() can be used to determine whether to subscribe. After subscribe(), Observable will hold the reference of Subscriber, so call this method in an appropriate place to prevent memory leakage.

Observable observable

  • create() create
Observable observable = Observable.create(new Observable.OnSubscribe<String>() { 
    @Override 
    public void call(Subscriber<? super String> subscriber) {
         subscriber.onNext("Hello"); 
        subscriber.onNext("Hi"); 
        subscriber.onNext("Aloha"); 
        subscriber.onCompleted(); 
    }
});
//Here, an OnSubscribe object is passed in as a parameter, and OnSubscribe will be stored in the returned
// The Observable object is equivalent to a schedule. When Observable is subscribed,
// The call() method of OnSubscribe will be called automatically, and the event sequence will be triggered in turn according to the settings.
  • just(T...) sends the incoming parameters in turn
Observable observable = Observable.just("Hello", "Hi", "Aloha");
// Will call in turn:
// onNext("Hello");
// onNext("Hi");
// onNext("Aloha");
// onCompleted();
  • From (t []) / from (Iterable <? Extends T >) splits the passed in array or Iterable into concrete objects and sends them out in turn
String[] words = {"Hello", "Hi", "Aloha"};
Observable observable = Observable.from(words);
// Will call in turn:
// onNext("Hello");
// onNext("Hi");
// onNext("Aloha");
// onCompleted();

Where just(T...) and from(T []) and create(OnSubscribe) are equivalent.

Subscribe

After creating Observable and Observer, they are linked with the subscribe() method

observable.subscribe(observer);
// Or:
observable.subscribe(subscriber);

Observer, subscribe to observer. When it is subscribed, it is when the subscribe() method executes

In addition to subscribe(Observer) and subscribe(Subscriber), subscribe() supports incompletely defined callbacks:

Action

Action1<String> onNextAction = new Action1<String>() {
    // onNext()
    @Override
    public void call(String s) {
        Log.d(tag, s);
    }
};
Action1<Throwable> onErrorAction = new Action1<Throwable>() {
    // onError()
    @Override
    public void call(Throwable throwable) {
        // Error handling
    }
};
Action0 onCompletedAction = new Action0() {
    // onCompleted()
    @Override
    public void call() {
        Log.d(tag, "completed");
    }
};

// Automatically create a Subscriber and use onNextAction to define onNext()
observable.subscribe(onNextAction);
// Automatically create a Subscriber, and use onNextAction and onErrorAction to define onNext() and onError()
observable.subscribe(onNextAction, onErrorAction);
// Automatically create a Subscriber and define onNext(), onError(), and onCompleted() using onNextAction, onErrorAction, and onCompletedAction
observable.subscribe(onNextAction, onErrorAction, onCompletedAction);
  • Action0
    Only call() has a callback method and has no parameters and no return value.

onCompleted() is a function with no parameters and no return, so Action0 can define incomplete callbacks

  • Action1
    Only call(T param) has a callback method and has a parameter and no return value.

onNext(T obj) and onError(Throwable error) have parameters, so Action1 can define incomplete callbacks

summary

RXjava is a good thing, but it is still difficult to learn. If you want to use it well, you can only practice it in practice.
These are my previous knowledge. I hope it will be useful to you. Finally, recommend a blog: Dry goods concentration camp

Topics: Java rxjava