Code Monkey home page Code Monkey logo

rxlife's Introduction

RxLife

API License

利用Jetpack下的Lifecycle获取Activity/Fragment的生命周期变化,通过 BehaviorSubject 转化成 Observable,从而通过Observable.takeUnit(otherObserable) 方法自动完成Observable与界面命周期绑定,在日常开发过程中,在该库的管控下,RxJava相关导致的内存泄漏率为0%(LeakCanary检测),强烈推荐使用。

为什么重复造轮子?

使用RxJava的开发者,一定知道trello/RxLifecycle项目,本项目LifecycleTransformer也是参考了trello/RxLifecycle,但是个人认为trello/RxLifecycle不够简洁,需要继承等等,所以在去年本人就开发了dhhAndroid/RxLifecycle,更加简洁地管理RxJava的生命周期;随着Google推出Jetpack,我发现在dhhAndroid/RxLifecycle库中获取界面生命周期的方式(通过在界面中注入空Fragment),在JetPack中的Lifecycle模块中已经实现,所以使用Kotlin结合Jetpack开发出了本项目。

安装

由于本库依赖于Jetpack,所以android.support库要在v7:26.1.x以上,例如:com.android.support:appcompat-v7:28.0.0

RxLife1对应RxJava1版本,RxLife2对应RxJava2版本

version:RxLife1: Download

    //support
    implementation 'com.android.support:appcompat-v7:28.0.0'
    //RxJava1
    implementation 'io.reactivex:rxjava:1.3.8'
    //Rxlife1
    implementation 'com.dhh:rxLife1:version'
    

version:RxLife2: Download

    //support
    implementation 'com.android.support:appcompat-v7:28.0.0'
    //RxJava2
    implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
    //Rxlife2
    implementation 'com.dhh:rxLife2:version'
    

使用(查看demo

前置条件:因support包中的 AppCompatActivity 和 android.support.v4.app.Fragment 均已实现 LifecycleOwner,所以确保你的项目 BaseActivity 继承于 AppCompatActivity,BaseFragment继承于 android.support.v4.app.Fragment。

RxLife1与RxLife2使用方式完全一样,以RxJava2版本为例:

标准用法(Java或Kotlin项目),使用RxLife.with(lifecycleOwner),以下compose在一个Observable事件流上使用一种即可:

        Observable.timer(10, TimeUnit.SECONDS)
                //标准使用模式,自动在[Lifecycle.Event.ON_DESTROY]注销
                .compose(RxLife.with(lifecycleOwner).bindToLifecycle())
                //指定在[Lifecycle.Event.ON_DESTROY]注销
                .compose(RxLife.with(lifecycleOwner).bindOnDestroy())
                //指定在某一生命周期注销,不常用
                .compose(RxLife.with(lifecycleOwner).bindUntilEvent(Lifecycle.Event.ON_DESTROY))
                .subscribe { Log.d("RxLife2-onCreate", it.toString()) }

通过Kotlin扩展函数使用,Kotlin项目推荐使用:

        Observable.timer(10, TimeUnit.SECONDS)
                //通过kotlin扩展方法使用,推荐;自动在[Lifecycle.Event.ON_STOP]注销
                .bindToLifecycle(lifecycleOwner)
                //指定在[Lifecycle.Event.ON_DESTROY]注销
                .bindonDestroy(lifecycleOwner)
                //指定在某一生命周期注销,不常用
                .bindUntilEvent(lifecycleOwner, Lifecycle.Event.ON_DESTROY)
                .subscribe { Log.d("RxLife2-onCreate", it.toString()) }

在MVP中使用,其实就是在使用的地方,能提供获取lifecycleOwner的方法即可,demo中有一个简易的MVP demo,详情查看demo。

rxlife's People

Contributors

dhhandroid avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.