Android Advanced: RxJava2 Source Resolution 1

This article is for use over Rxjava2 or understand basic usage Rxjava2 students reading

What is a .Rxjava

Rxjava on GitHub homepage introduces himself as "a library for composing asynchronous and event-based programs using observable sequences for the Java VM" (a use sequence can be observed on the Java VM to compose asynchronous library program event-based ).

Colloquially, Rxjava a frame using the observer pattern designed to handle asynchronous. Chained calls designed to make the code readable and elegant.

for example:
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("a");
            }
        });

        observable.subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

This is the simplest use of Rxjava2:
1. Create a Observable, rewrite subscribe method, here mainly deal with the observed events.
2. Subscribe to this Observable, event observer callback method, we can do a deal with the response to the event

Source resolve two .Rxjava

2.1 Creating Observable:

Observable created using a Observable.create (ObservableOnSubscribe <T> source) method. The parameters of this method is ObservableOnSubscribe:

public interface ObservableOnSubscribe<T> {

    /**
     * Called for each Observer that subscribes.
     * @param e the safe emitter instance, never null
     * @throws Exception on error
     */
    void subscribe(@NonNull ObservableEmitter<T> e) throws Exception;
}

ObservableOnSubscribe is an interface, only Subscribe method, parameters are ObservableEmitter <T> e. Emitter ObservableEmitter is inherited interfaces, the interface defined in the Emitter onNext, onError, onComplete the like, and the Observer (observer) of a corresponding method.

public interface Emitter<T> {

    /**
     * Signal a normal value.
     * @param value the value to signal, not null
     */
    void onNext(@NonNull T value);

    /**
     * Signal a Throwable exception.
     * @param error the Throwable to signal, not null
     */
    void onError(@NonNull Throwable error);

    /**
     * Signal a completion.
     */
    void onComplete();
}

ObservableEmitter Interface Emitter to be expanded to include setDisposable, setCancellable methods such as
the basic parameters of understanding, and now look at what has been done inside the create method, the code is as follows:

public static <T> Observable<T> create(ObservableOnSubscribe<T> source) {
        return RxJavaPlugins.onAssembly(new ObservableCreate<T>(source));
    }

Call RxJavaPlugins of onAssembly method. We have a new parameter ObservableCreate <T> (source), we see what it is:

final class ObservableCreate<T> extends Observable<T> {

    public ObservableCreate(ObservableOnSubscribe<T> source) {
        this.source = source;
    }

}

Inherited Observable, it is also observed objects in the constructor we see our new ObservableOnSubscribe objects, and by the presence of the ObservableCreate source inside
that we continue to look at ways to do what onAssembly:

public static <T> Observable<T> onAssembly(@NonNull Observable<T> source) {
        Function<? super Observable, ? extends Observable> f = onObservableAssembly;
        if (f != null) {
            return apply(f, source);
        }
        return source;
    }

Hook a method. onObservableAssembly is a static variable, we did not set, the default is empty, it is returned directly source object. In other words, create a method Observable is actually put us ObservableOnSubscribe objects stored in the source ObservableCreate object inside, and then return ObservableCreate object.
We know ObservableCreate inherited Observable, it creates ObservableCreate objects, we will create Observable finished.

2.2 subscribe to an event (the observer)

Subscribe to the viewer operation is observable.subscribe (new Observer <String> ( )). This operator is actually a "passive", that is, the event is observed by the observer. Because subscribe method where the parameter is the Observer observer. We also receive events related to the return value in the Observer in each method will be in tune.
We look at the source subscribe method:

   public final void subscribe(Observer<? super T> observer) {
        try {
            subscribeActual(observer);
        } catch (NullPointerException e) { // NOPMD
            throw e;
        } catch (Throwable e) {
            RxJavaPlugins.onError(e);
        }
    }

Look at the code we know the most important method call is: subscribeActual (observer) ;, Observable in this method is abstract method, but this time we are Observable (front create an object method returns) a ObservableCreate object. So we take a look inside ObservableCreate how to override this method. code show as below:

 protected void subscribeActual(Observer<? super T> observer) {
        CreateEmitter<T> parent = new CreateEmitter<T>(observer);
        observer.onSubscribe(parent);

        try {
            source.subscribe(parent);
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            parent.onError(ex);
        }
    }

We saw this method is mainly to do three things:
① create a CreateEmitter target parent.
② the parent pass subscribe method of source. We know the source above is just ObservableOnSubscribe objects stored, subscribe our method is overridden:

          @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("a");
            }

So we will be able to receive a CreateEmmiter In this method, by CreateEmitter can callback the appropriate method. CreateEmitter is to achieve ObservableEmitter interface, we look at its internal implementation, such as: onNext source code as follows:

@Override
public void onNext(T t) {
    observer.onNext(t);
}

In other words, when we call onNext method of ObservableEmitter in subscribe ObservableOnSubscribe's method, which it calls the onNext observer. So by such a transfer, we will be able to observer callback method in response to receipt of the relevant state in the event.

Thus a simple Rxjava streaming transfer principle has been finished, summarize the process is as follows:
  • Observbable.create method used to produce a ObservableCreate object, the object ObservableOnSubscribe deposit with the object source.
  • Call ObservableCreate.subscribe method, the actual call is subscribeActual method, passing a Observer object.
  • subscribeActual method to create a CreateEmmiter object, call source.subscribe method, passing CreateEmmiter object.
  • So we received ObservableOnSubscribe into a CreateEmmiter, CreateEmmiter is a subclass of ObservableEmmiter. We can call the method CreateEmmiter here for event callbacks.
  • Call CreateEmmiter method actually calls the method of response Observer. That is CreateEmmiter pass the event to the observer status.

Guess you like

Origin blog.51cto.com/14332859/2413759