RxJava2修炼之路(一)

RxJava2修炼之路——初尝禁果

RxJava问世已经好一段时间了,网上评论也会是一片叫好,目前最流行的项目架构也是离开不了它:RxJava + Retrofit +okhttp,以前在CSDN上面写过一片关于RxJava的文章,写的只是一些基本操作原理,也没有深入了解。

RxJava的具体设计思路

RxJava是类似观察者模式的代码结构编写的,本文也是在观察者模式的基础上写的,如果不是很了解什么是观察者模式,可以查看资料或者可以查看本人在CSDN上的一片关于观察者模式的文章,RxJava之所以被大家喜爱,首先就是它的流式编程,采用构建者模式来一体式添加所有的操作。

编写流程

先来写一个简单的例子:

Observable observable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {
                   e.onNext("我");
                   e.onNext("来");
                   e.onNext("了");
                   e.onNext("RxJava2");
                   e.onComplete();
            }
        });
        Observer observer = new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "onSubscribe: "+d.isDisposed());
            }

            @Override
            public void onNext(Object value) {
                Log.d(TAG, "onNext: 接收到值"+value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: 出错了,终止接受了");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete: 完成接受,终止");
            }
        };
        observable.subscribe(observer);

打印结果:

09-02 06:10:05.322 12580-12580/? D/tag: onSubscribe: false
09-02 06:10:05.322 12580-12580/? D/tag: onNext: 接收到值我
09-02 06:10:05.322 12580-12580/? D/tag: onNext: 接收到值来
09-02 06:10:05.322 12580-12580/? D/tag: onNext: 接收到值了
09-02 06:10:05.322 12580-12580/? D/tag: onNext: 接收到值RxJava2
09-02 06:10:05.322 12580-12580/? D/tag: onComplete: 完成接受,终止

代码中首先创建了一个被观察者Observable,在Observable的subscribe方法中包含一个ObservableEmitter对象,这就像一个发射器一样发射事件,该对象含有四个重要的也是我们经常要用到的方法:

  • onNext(Object o)
    参数作为需要发送的数据,可以调用多次。
  • onError(Throwable e)
    当发送事件过程中出现异常时调用,该方法调用后结束发送,就算没有发送完也不再会发送。
  • onComplete()
    当OnNext中的所有事件完成发送时回调。
  • onSubscribe(Disposable d)
    该方法在被观察者订阅观察者的时候回调,它执行在其他三个方法的前面,而Disposable是一个用来管理是否终止事件发送的开关,可以将该对象赋值然后在观察者的接受事件的方法中进行处理,比如,当接受到“”字的时候终止接受事件。
Observer observer = new Observer() {
            Disposable mDisposable;

            @Override
            public void onSubscribe(Disposable d) {
                mDisposable = d;
                Log.d(TAG, "onSubscribe: " + d.isDisposed());
            }

            @Override
            public void onNext(Object value) {
                if (value.equals("了")) {
                    mDisposable.dispose();
                }
                Log.d(TAG, "onNext: 接收到值" + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "onError: 出错了,终止接受了");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "onComplete: 完成接受,终止");
            }
        };

打印结果:

09-02 06:34:26.845 1850-1850/ruanrong.com.rxjava2demo D/tag: onSubscribe: false
09-02 06:34:26.846 1850-1850/ruanrong.com.rxjava2demo D/tag: onNext: 接收到值我
09-02 06:34:26.846 1850-1850/ruanrong.com.rxjava2demo D/tag: onNext: 接收到值来
09-02 06:34:26.846 1850-1850/ruanrong.com.rxjava2demo D/tag: onNext: 接收到值了

可以看到当调用mDisposable.dispose()方法后,观察者将不再接收到被观察者发送的任何事件包括onError和OnCompleted。

对于上面的代码有更简单的一个写法:

 Observable.just("我","来","了","RxJava2").subscribe(new Consumer() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, "accept: "+s);
            }
        });

结果是一样的,是不是很神奇啊,其实just方法需要是数组参数,然后内部将这些数组中的值一个一个发送出去而已,那Consume又是什么东西呢?它和Observer啥关系

package io.reactivex.functions;

/**
 * A functional interface (callback) that accepts a single value.
 * @param  the value type
 */
public interface Consumer {
    /**
     * Consume the given value.
     * @param t the value
     * @throws Exception on error
     */
    void accept(T t) throws Exception;
}

public interface Observer {

    /**
     * Provides the Observer with the means of cancelling (disposing) the
     * connection (channel) with the Observable in both
     * synchronous (from within {@link #onNext(Object)}) and asynchronous manner.
     * @param d the Disposable instance whose {@link Disposable#dispose()} can
     * be called anytime to cancel the connection
     * @since 2.0
     */
    void onSubscribe(Disposable d);

    /**
     * Provides the Observer with a new item to observe.
     * 

* The {@link Observable} may call this method 0 or more times. *

* The {@code Observable} will not call this method again after it calls either {@link #onComplete} or * {@link #onError}. * * @param value * the item emitted by the Observable */ void onNext(T value); /** * Notifies the Observer that the {@link Observable} has experienced an error condition. *

* If the {@link Observable} calls this method, it will not thereafter call {@link #onNext} or * {@link #onComplete}. * * @param e * the exception encountered by the Observable */ void onError(Throwable e); /** * Notifies the Observer that the {@link Observable} has finished sending push-based notifications. *

* The {@link Observable} will not call this method if it calls {@link #onError}. */ void onComplete(); }

没错,Consumer和Observer都是一个接口,既然他是不同的接口,那就看源码呗:

   @SchedulerSupport(SchedulerSupport.NONE)
    public final Disposable subscribe(Consumer onNext, Consumer onError,
            Action onComplete, Consumer onSubscribe) {
        ObjectHelper.requireNonNull(onNext, "onNext is null");
        ObjectHelper.requireNonNull(onError, "onError is null");
        ObjectHelper.requireNonNull(onComplete, "onComplete is null");
        ObjectHelper.requireNonNull(onSubscribe, "onSubscribe is null");

        LambdaObserver ls = new LambdaObserver(onNext, onError, onComplete, onSubscribe);

        subscribe(ls);

        return ls;
    }

    @SchedulerSupport(SchedulerSupport.NONE)
    @Override
    public final void subscribe(Observer observer) {
        ObjectHelper.requireNonNull(observer, "observer is null");
        try {
            observer = RxJavaPlugins.onSubscribe(this, observer);

            ObjectHelper.requireNonNull(observer, "Plugin returned null Observer");

            subscribeActual(observer);
        } catch (NullPointerException e) { // NOPMD
            throw e;
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            // can't call onError because no way to know if a Disposable has been set or not
            // can't call onSubscribe because the call might have set a Subscription already
            RxJavaPlugins.onError(e);

            NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
            npe.initCause(e);
            throw npe;
        }
    }

我截取了这两个方法的源码,在subscribe(Consumer onNext, Consumer onError,
Action onComplete, Consumer onSubscribe)调用有这样一行代码:

 LambdaObserver ls = new LambdaObserver(onNext, onError, onComplete, onSubscribe);
// onNext 就是Consumer对象
subscribe(ls);

点进去发现 LambdaObserver是实现了Observer的一个将Consumer对象转换成了Observer的类,原来如此。这下就应该很清楚了吧,Consumer也是一个只有接受事件的观察者。

总结

RxJava其实就是创建一个观察者对象,再创建一个被观察者对象,然后被观察者调用内部的发射器不断发射事件给订阅了被观察者的观察者,观察者内部的毁掉方法实时接受到被观察者发送的事件进行处理。

你可能感兴趣的:(RxJava2修炼之路(一))