在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:rxjava-RxLife开源软件地址:https://gitee.com/327744707/rxjava-RxLife开源软件介绍:RxHttp&RxLife 交流群:378530627 个人微信:ljx-studio RxLifeRxLife,相较于trello/RxLifecycle、uber/AutoDispose,具有如下优势:
友情提示: RxLife与RxHttp搭配使用,味道更佳 RxLife详细介绍:https://juejin.im/post/5cf3e1235188251c064815f1 Gradle引用 将 allprojects { repositories { maven { url "https://jitpack.io" } }}
新版本仅支持AndroidX项目 dependencies { //rxjava2 implementation 'com.github.liujingxing.rxlife:rxlife-rxjava2:2.2.1' //rxjava3 implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.2.1'} 注意: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/FragmentFragmentActivity/Fragment销毁时,自动关闭RxJava管道 Observable.timer(5, TimeUnit.SECONDS) .as(RxLife.as(this)) //此时的this FragmentActivity/Fragment对象 .subscribe(aLong -> { Log.e("LJX", "accept =" + aLong); }); 2、ViewView被移除时,自动关闭RxJava管道 Observable.timer(5, TimeUnit.SECONDS) .as(RxLife.as(this)) //此时的this 为View对象 .subscribe(aLong -> { Log.e("LJX", "accept =" + aLong); }); 3、ViewModelActivity/Fragment销毁时,自动关闭RxJava管道,ViewModel需要继承 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管道,任意类需要继承 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用户由于 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) } 使用 6、小彩蛋asOnMain操作符RxLife还提供了 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 用户使用 注意: RxLife类里面as操作符,皆适用于Flowable、ParallelFlowable、Observable、Single、Maybe、Completable这6个被观察者对象 混淆RxLife作为开源库,可混淆,也可不混淆,如果不希望被混淆,请在proguard-rules.pro文件添加以下代码 -keep class com.rxjava.rxlife.**{*;} 更新日志2.2.12021-09-05
2.2.02021-09-01
2.1.02021-05-11
2.0.0
1.1.0
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
LicensesCopyright 2019 liujingxingLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License. |
请发表评论