rxjava-RxLife

Introduction: 一行代码解决 RxJava 内存泄漏,一款轻量级别的 RxJava 生命周期管理库
More: Author   ReportBugs   
Tags:

RxHttp&RxLife 交流群:378530627      个人微信:ljx-studio

RxLife,相较于trello/RxLifecycleuber/AutoDispose,具有如下优势:

  • 直接支持在主线程回调
  • 支持在子线程订阅观察者
  • 简单易用,学习成本低
  • 性能更优,在实现上更加简单

友情提示: RxLife 与RxHttp搭配使用,味道更佳

RxLife 详细介绍:https://juejin.im/post/5cf3e1235188251c064815f1

Gradle 引用

jitpack添加到项目的build.gradle文件中,如下:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

注:RxLife 2.1.0 版本起,已全面从 JCenter 迁移至 jitpack

新版本仅支持 AndroidX 项目

dependencies {
   //rxjava2
   implementation 'com.github.liujingxing.rxlife:rxlife-rxjava2:2.2.2'

   //rxjava3
   implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.2.2'
}

注意:RxJava2 使用 Rxlife.asXxx 方法;RxJava3 使用 Rxlife.toXxx 方法

非 AndroidX 项目

非 AndroidX 项目,请使用旧版本 RxLife

implementation 'com.rxjava.rxlife:rxlife:2.0.0'

由于 Google 在 19 年就停止了非 AndroidX 库的更新,故 rxlife 旧版本不再维护,请尽快将项目迁移至 AndroidX

Usage

1、FragmentActivity/Fragment

FragmentActivity/Fragment 销毁时,自动关闭 RxJava 管道

Observable.timer(5, TimeUnit.SECONDS)
    .as(RxLife.as(this))     //此时的 this FragmentActivity/Fragment 对象
    .subscribe(aLong -> {
        Log.e("LJX", "accept =" + aLong);
    });

2、View

View 被移除时,自动关闭 RxJava 管道

Observable.timer(5, TimeUnit.SECONDS)
    .as(RxLife.as(this))  //此时的 this 为 View 对象
    .subscribe(aLong -> {
        Log.e("LJX", "accept =" + aLong);
    });

3、ViewModel

Activity/Fragment 销毁时,自动关闭 RxJava 管道,ViewModel 需要继承ScopeViewModel类,如下

public class MyViewModel extends ScopeViewModel {

    public MyViewModel(@NonNull Application application) {
        super(application);
    }

    public void test(){
        Observable.interval(1, 1, TimeUnit.SECONDS)
            .as(RxLife.asOnMain(this))  //继承 ScopeViewModel 后,就可以直接传 this
            .subscribe(aLong -> {
                Log.e("LJX", "MyViewModel aLong=" + aLong);
            });
    }
}

注意: 一定要在 Activity/Fragment 通过以下方式获取 ViewModel 对象,否则 RxLife 接收不到生命周期的回调


MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

4、任意类

Activity/Fragment 销毁时,自动关闭 RxJava 管道,任意类需要继承BaseScope类,如 P 层:

public class Presenter extends BaseScope {

    public Presenter(LifecycleOwner owner) {
        super(owner); //添加生命周期监听
    }

    public void test(){
        Observable.interval(1, 1, TimeUnit.SECONDS)
            .as(RxLife.as(this)) //继承 BaseScope 后,就可以直接传 this
            .subscribe(aLong -> {
                Log.e("LJX", "accept aLong=" + aLong);
            });
    }
}

5、kotlin 用户

由于as是 kotlin 中的一个关键字,所以在 kotlin 中,我们并不能直接使用as(RxLife.as(this)),可以如下编写

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
    .`as`(RxLife.`as`(this))
    .subscribe { aLong ->
        Log.e("LJX", "accept=" + aLong)
    }

当然,相信没多少人会喜欢这种写法,故,RxLife 针对 kotlin 用户,新增更为便捷的写法,如下:

Observable.intervalRange(1, 100, 0, 200, TimeUnit.MILLISECONDS)
    .life(this)
    .subscribe { aLong ->
        Log.e("LJX", "accept=" + aLong)
    }

使用life 操作符替代as操作符即可,其它均一样

6、小彩蛋

asOnMain 操作符

RxLife 还提供了asOnMain操作符,它可以指定下游的观察者在主线程中回调,如下:

Observable.timer(5, TimeUnit.SECONDS)
    .as(RxLife.asOnMain(this))
    .subscribe(aLong -> {
        //在主线程回调
       Log.e("LJX", "accept =" + aLong);
    });

        //等价于
Observable.timer(5, TimeUnit.SECONDS)
    .observeOn(AndroidSchedulers.mainThread())
    .as(RxLife.as(this))
    .subscribe(aLong -> {
        //在主线程回调
        Log.e("LJX", "accept =" + aLong);
    });

kotlin 用户使用lifeOnMain替代asOnMain操作符,其它均一样

注意: RxLife 类里面 as 操作符,皆适用于 Flowable、ParallelFlowable、Observable、Single、Maybe、Completable 这 6 个被观察者对象

混淆

RxLife 作为开源库,可混淆,也可不混淆,如果不希望被混淆,请在 proguard-rules.pro 文件添加以下代码

-keep class com.rxjava.rxlife.**{*;}

更新日志

2.2.2

2022-04-01

  • Kotlin升级至v1.6.10

  • Lifecycle升级至v2.4.1

2.2.1

2021-09-05

  • 适配RxJava v3.1.1 版本RxLife v2.2.1版本部分API不向下兼容,升级该版本的同时,同步升级 RxJava 版本至 v3.1.1 不向下兼容的API:Completable + RxLifeParallelFlowable + RxLife

2.2.0

2021-09-01

  • Kotlin 升级到 1.5.30

  • rxLifeScope 标记为过时,正式退出历史舞台

    2.1.0

    2021-05-11

  • 新增:RxLife类下新增dispose(Disposable)、isDisposed(Disposable)静态方法

  • 修改:由于 JCenter 停止维护,故全面迁移至 jitpack,groupId由之前com.ljx.rxlife改为com.github.liujingxing.rxlife,版本升级时,请注意更改依赖

2.0.0

  • 新增 RxLifeScope 类,用于开启协程,并在 FragmentActivity/ViewModel 环境下可以自动关闭协程

  • rxlife-x 的 lifecycle 等组件升级到 2.2.0 版本

1.1.0

  • RxLife 类增加 as(View,boolean)、asOnMain(View,boolean)方法

  • 删除过时的 lift、compose 等方法

1.0.9

  • kotlin 中,支持在 ViewModel 及任意类使用 life、lifeOnMain 操作符

1.0.8

  • 修复在 Activity/Fragment 中,子线程订阅事件时,偶现观察者没有回调问题

1.0.7

  • BaseScope、ScopeViewModel 两个类加入 RxLife 此版本中

1.0.6

  • 代码优化

1.0.5

  • 引入作用域的概念,支持在 View 中自动中断 RxJava 管道

  • 对 Kotlin 简单适配,在 kotlin 中可使用 life 操作符绑定生命周期

1.0.4

  • 新增 as 操作符,规定下游只能使用 subscribe 操作符

  • lift、compose 标记为过时,在未来的版本中将会删除,请使用 as 操作符替代

Licenses

Copyright 2019 liujingxing

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools