添加依赖

    implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
    implementation 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.1'

在rxlifecycle依赖包下游如下几个关键类

  • RxLifecycle
  • LifecycleProvider
  • LifecycleTransformaer

在rxlifecycle-android-lifecycle依赖包下有如下几个关键类

  • AndroidLifecycle
  • RxLifecycleAndroidLifecycle

不直接使用RxLifecycle,而是使用AndroidLifecycle.

如何创建AndroidLifecycle.

//AndroidLifecycle.java
LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(this);

AndroidLifecycle实现了android.arch.lifecycle.LifecycleObserver.可以通过注解@OnLifecycleEvent来监听对应的Lifecycle.Event.

 @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    void onEvent(LifecycleOwner owner, Lifecycle.Event event) {
    //向下游传递
        lifecycleSubject.onNext(event);
        if (event == Lifecycle.Event.ON_DESTROY) {
        //解除观察者
            owner.getLifecycle().removeObserver(this);
        }
    }

这里的lifecycleSubject是一个BehaviorSubject对象.既可以作为观察者,也可以作为被观察对象使用.当监听到对应的Lifecycle.Event时,就会通过lifecycleSubject.onNext(event);向下游传递.

AndroidLifecycle中定义了两个绑定相关的方法.通过这两个绑定方法,将lifecycleSubject与下游连接起来,才能确保lifecycleSubject携带的信息能传递给下游.

    /**
    * 绑定某个具体的生命周期环节
    *  event具体为
    *   ON_CREATE
    *   ON_START
    *   ON_RESUME
    *   ON_PAUSE
    *   ON_STOP
    *   ON_DESTROY
    *   ON_ANY
    */
    public <T> LifecycleTransformer<T> bindUntilEvent(@NonNull Lifecycle.Event event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    /**
    * 绑定到生命周期
    */
    public <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroidLifecycle.bindLifecycle(lifecycleSubject);
    }

第一个方法使用到了RxLifecycle.bindUntilEvent方法.

public static <T, R> LifecycleTransformer<T> bindUntilEvent(@Nonnull final Observable<R> lifecycle,
                                                              @Nonnull final R event) {
      checkNotNull(lifecycle, "lifecycle == null");
      checkNotNull(event, "event == null");
      return bind(takeUntilEvent(lifecycle, event));
  }

这里的takeUntilEvent方法是判断lifecycle所携带的event是否与参数event一致.bind方法源码如下:

 public static <T, R> LifecycleTransformer<T> bind(@Nonnull final Observable<R> lifecycle) {
        return new LifecycleTransformer<>(lifecycle);
    }

创建了一个LifecycleTransformer对象.

public final class LifecycleTransformer<T> implements ObservableTransformer<T, T>,
                                                      FlowableTransformer<T, T>,
                                                      SingleTransformer<T, T>,
                                                      MaybeTransformer<T, T>,
                                                      CompletableTransformer{
    final Observable<?> observable;

    LifecycleTransformer(Observable<?> observable) {
        checkNotNull(observable, "observable == null");
        this.observable = observable;
    }

    @Override
    public ObservableSource<T> apply(Observable<T> upstream) {
        return upstream.takeUntil(observable);
    }

    @Override
    public Publisher<T> apply(Flowable<T> upstream) {
        return upstream.takeUntil(observable.toFlowable(BackpressureStrategy.LATEST));
    }

    @Override
    public SingleSource<T> apply(Single<T> upstream) {
        return upstream.takeUntil(observable.firstOrError());
    }

    @Override
    public MaybeSource<T> apply(Maybe<T> upstream) {
        return upstream.takeUntil(observable.firstElement());
    }

    @Override
    public CompletableSource apply(Completable upstream) {
        return Completable.ambArray(upstream, observable.flatMapCompletable(Functions.CANCEL_COMPLETABLE));
    }

                                                      }

先说一下takeUntil操作符的作用.

 ObservableA.takeUntil(ObservableB);

ObservableB开始发射数据,ObservableA停止发射数据.
那么对于LifecycleTransformer,当observable开始发射数据,
upstream就会停止发射数据.这里的observable就是AndroidLifecycle中的BehaviorSubject.而upstream就是我们自己的数据源.

LifecycleTransformer通过与RxJava2的操作符compose结合使用.

 val lifecycleProvider = AndroidLifecycle.createLifecycleProvider(this)

observable.compose(lifecycleProvider.bindUntilEvent(Lifecycle.Event.ON_DESTROY))

idealcn
27 声望4 粉丝

引用和评论

0 条评论