阅读 262

Android RxLife 一款轻量级别的RxJava生命周期管理库(二)

Android RxLife 一款轻量级别的RxJava生命周期管理库(一)一文中,我们讲解了lift,compose这两个操作符的使用,及RxJava是如何造成内存泄漏的,RxLife又是如何解决RxJava内存泄漏的。在这不再次进行讲解,如果还没有看的,请回头阅读

本文主要讲解RxLife在最新版本1.0.4中的as操作符的使用及其作用。

gradle依赖

implementation 'com.rxjava.rxlife:rxlife:1.0.4'
复制代码

源码下载 欢迎star

看过上篇文章的同学知道,在使用lift,compose这两个操作符时,我们规定了下游除了subscribe操作符外不能有其它的操作符出现,然后这种规定不是代码层面的规定,需要开发者在开发时额外注意,显然这种方案是不完美的,那么我们能不能从代码层面上去规定呢?此时就要请我们主角出场了!

as操作符

我们先来看看as操作符如何使用。(以下代码默认运行在Activity中)

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        .as(RxLife.as(this)) //this 为LifecycleOwner对象
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong);
        });
复制代码

这样就可以了?是的,就这么简单。那么as是如何规避下游不会出现除subscribe之外的操作符呢?我们先来看看使用as操作符,返回的啥?

ObservableLife<Long> observableLife = Observable
        .intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        .as(RxLife.asOnMain(this));
复制代码

返回的是一个ObservableLife对象,这是什么鬼?进去看看

/**
 * User: ljx
 * Date: 2019/4/18
 * Time: 18:40
 */
public class ObservableLife<T> extends RxSource<Observer<? super T>> {

    private Observable<T> upStream;

    ObservableLife(Observable<T> upStream, LifecycleOwner owner, Event event, boolean onMain) {
        super(owner, event, onMain);
        this.upStream = upStream;
    }

    @Override
    public final Disposable subscribe() {
        return subscribe(Functions.emptyConsumer(), Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION, Functions.emptyConsumer());
    }

    public final Disposable subscribe(Consumer<? super T> onNext) {
        return subscribe(onNext, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION, Functions.emptyConsumer());
    }

    public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) {
        return subscribe(onNext, onError, Functions.EMPTY_ACTION, Functions.emptyConsumer());
    }

    public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError,
                                      Action onComplete) {
        return subscribe(onNext, onError, onComplete, Functions.emptyConsumer());
    }

    public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError,
                                      Action onComplete, Consumer<? super Disposable> 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<T> ls = new LambdaObserver<T>(onNext, onError, onComplete, onSubscribe);

        subscribe(ls);

        return ls;
    }


    @Override
    public final void subscribe(Observer<? super T> observer) {
        ObjectHelper.requireNonNull(observer, "observer is null");
        try {
            observer = RxJavaPlugins.onSubscribe(upStream, observer);

            ObjectHelper.requireNonNull(observer, "The RxJavaPlugins.onSubscribe hook returned a null Observer. Please change the handler provided to RxJavaPlugins.setOnObservableSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins");

            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;
        }
    }

    private void subscribeActual(Observer<? super T> observer) {
        Observable<T> upStream = this.upStream;
        if (onMain) {
            upStream = upStream.observeOn(AndroidSchedulers.mainThread());
        }
        upStream.onTerminateDetach().subscribe(new LifeObserver<>(observer, owner, event));
    }
}
复制代码

仔细的你会发现,ObservableLife类里面就只有一系列的subscribe方法,而且跟Observable里面的subscribe方法是一一对应的,功能也是一样的。到这,我们就明白了,原来as操作符返回的是一个自定义的对象,而且该对象只提供了一系列subscribe方法,这样就规避了下游不会再出现其它的操作符,就避免内存泄漏等危险。

到这,as操作符的原理我们就讲清楚了,我们再来看看as操作符的其它用法。

指定生命周期关闭管道

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        //指定在onStop关闭管道,也可以指定其它生命周期,不指定默认在onDestroy关闭管道
        .as(RxLife.as(this, Event.ON_STOP)) 
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong + " Thread=" + Thread.currentThread());
        });
复制代码

指定观察者在主线程回调

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        .as(RxLife.asOnMain(this)) //asOnMain即代表在主线程回调
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong + " Thread=" + Thread.currentThread());
        });
        
        //等价于
Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .as(RxLife.as(this))
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong + " Thread=" + Thread.currentThread());
        });
复制代码

同时指定线程回调及生命周期

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        //在主线程回调,并指定在onStop关闭管道,也可以指定其它生命周期,不指定默认在onDestroy关闭管道
        .as(RxLife.asOnMain(this, Event.ON_STOP)) 
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong + " Thread=" + Thread.currentThread());
        });
        //等价于
Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .as(RxLife.as(this, Event.ON_STOP))
        .subscribe(aLong -> {
            Log.e("LJX", "accept=" + aLong + " Thread=" + Thread.currentThread());
        });
复制代码

ok,as操作符用法就这么多,如果有更好的idea,请告诉我。

小彩蛋

RxLife类里面的as系列方法,皆适用于Observable、Flowable、ParallelFlowable、Single、Maybe、Completable这6个被观察者对象,道理都一样,这里不在一一讲解。

有疑问,请留言,我会在第一时间作答。

扩展

RxLife结合RxHttp发送请求,简直不要太爽。

Rxhttp详情请点击RxHttp 一条链发送请求,新一代Http请求神器(一)

关注下面的标签,发现更多相似文章
评论