All Projects → liujingxing → Rxjava Rxlife

liujingxing / Rxjava Rxlife

Licence: apache-2.0
一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Rxjava Rxlife

Tdcapp
Sample app which access the TDC (The Developer's Conference) REST API.
Stars: ✭ 55 (-79.55%)
Mutual labels:  rxjava2, lifecycle, viewmodel
TeamManagerApp
A sample app structure using the MVVM architecture LiveData, RxJava, ViewModel, Room and the Navigation Arch Components.
Stars: ✭ 36 (-86.62%)
Mutual labels:  rxjava2, viewmodel, lifecycle
rxlife
一行代码解决RxJava 内存泄漏,一款轻量级别的RxJava生命周期管理库
Stars: ✭ 322 (+19.7%)
Mutual labels:  rxjava2, viewmodel, lifecycle
Mvvmarms
Android MVVM Architecture Components based on MVPArms and Android Architecture Components.
Stars: ✭ 425 (+57.99%)
Mutual labels:  rxjava2, lifecycle, viewmodel
MockAppMVVM
A sample app structure using the MVVM architecture using Retrofit, Dagger2, LiveData, RxJava, ViewModel and Room.
Stars: ✭ 14 (-94.8%)
Mutual labels:  rxjava2, viewmodel, lifecycle
searchi
Demo of PagedListAdapter, PageKeyedDataSource, LiveData, ViewModel in Kotlin
Stars: ✭ 13 (-95.17%)
Mutual labels:  rxjava2, viewmodel
Pursuit-Core-Android
Pursuit Core Android
Stars: ✭ 45 (-83.27%)
Mutual labels:  rxjava2, viewmodel
Sunset-hadith
Islamic app written with Kotlin, using KTOR + coroutines + flow + MVVM + Android Jetpack + Navigation component. Old version using RxJava + Retrofit + OKHttp
Stars: ✭ 26 (-90.33%)
Mutual labels:  rxjava2, viewmodel
GithubApp-android-architecture
Let's learn a deep look at the Android architecture
Stars: ✭ 16 (-94.05%)
Mutual labels:  rxjava2, viewmodel
Disposer
Easily dispose rxJava streams with Android's Lifecycle
Stars: ✭ 176 (-34.57%)
Mutual labels:  rxjava2, lifecycle
Newsster
Android App using Paging3, Hilt, Coroutines, Flow, Jetpack, MVVM architecture.
Stars: ✭ 147 (-45.35%)
Mutual labels:  viewmodel, lifecycle
AndriodDevelopLibrary
Lifecycle->ViewModel+LiveData+Repository
Stars: ✭ 16 (-94.05%)
Mutual labels:  viewmodel, lifecycle
LifecycleMvp
No description or website provided.
Stars: ✭ 20 (-92.57%)
Mutual labels:  viewmodel, lifecycle
Clean-MVVM-NewsApp
Android News app developed using Clean + MVVM architecture
Stars: ✭ 52 (-80.67%)
Mutual labels:  rxjava2, viewmodel
RestaurantsExplorer
Android application build with MVVM Pattern, using Zomato API to enable search cities arround the world and display the city restaurants on a map.
Stars: ✭ 32 (-88.1%)
Mutual labels:  rxjava2, viewmodel
DaggerExoPlayer
This repository demonstrates ExoPlayer injection with Dagger2
Stars: ✭ 58 (-78.44%)
Mutual labels:  rxjava2, viewmodel
StackOverFlowApi
working with Stack OverFlow Api
Stars: ✭ 24 (-91.08%)
Mutual labels:  rxjava2, viewmodel
Android-Mvi-Starter
Android MVI Starter application
Stars: ✭ 19 (-92.94%)
Mutual labels:  rxjava2, viewmodel
PlayAndroid
✌️✊👋玩安卓Mvvm组件化客户端,整合Jetpack组件DataBinding、ViewModel以及LiveData;屏幕适配✔️状态栏沉浸式✔️黑夜模式✔️,无数据、加载失败状态页;骨架屏、Koin依赖注入等
Stars: ✭ 193 (-28.25%)
Mutual labels:  rxjava2, viewmodel
Roomrxjava
Room with Rxjava Example
Stars: ✭ 130 (-51.67%)
Mutual labels:  rxjava2, viewmodel

Download Download

RxLife

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

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

RxHttp&RxLife 交流群:378530627

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

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

Gradle引用

新版本仅支持AndroidX项目

dependencies {
   //rxjava2
   implementation 'com.ljx.rxlife2:rxlife-rxjava:2.0.0'
       
   //rxjava3
   implementation 'com.ljx.rxlife3:rxlife-rxjava:3.0.0'
}

注意:rxlife2 使用Rxlife.asXxx方法;rxlife3使用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.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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].