项目作者: sellmair

项目描述 :
Easily dispose rxJava streams with Android's Lifecycle
高级语言: Kotlin
项目地址: git://github.com/sellmair/disposer.git
创建时间: 2018-09-29T12:03:40Z
项目社区:https://github.com/sellmair/disposer

开源协议:MIT License

下载


Disposer

Easily dispose RxJava streams with Android’s Lifecycle

GitHub top language
Build Status
Bintray

Checkout my @sellmair/disposing-on-android-the-right-way-97bd55cbf970">Medium article.

Usage

Gradle
  1. dependencies {
  2. // Non AndroidX projects
  3. implementation 'io.sellmair:disposer:1.1.0'
  4. // AndroidX projects rxjava2
  5. implementation 'io.sellmair:disposer:2.0.0'
  6. // AndroidX projects rxjava3
  7. implementation 'io.sellmair:disposer:3.0.0'
  8. }


Disposer

A Disposer is the object managing multiple Disposable instances and disposes them
at the correct time.

You can easily add a given disposable to a Disposer:

  1. val disposer: Disposer = /* ... */
  2. val disposable = Service.queryAwesomeData().subscribe()
  3. disposer.add(disposable) // The disposable will now managed by the disposer

Or a much sweeter apis, that might look familiar to rxKotlin users:

  1. val disposer: Disposer = /* ... */
  2. disposer += Service.queryAwesomeData().subscribe() // Managed by the disposer
  1. val disposer: Disposer = /* ... */
  2. Service.queryAwesomeData().subscribe().disposeBy(disposer) // Managed by the disposer


Get the correct Disposer

RxLifecycle makes it easy to get a disposer for each lifecycle hook like
onCreate, onStart, onResume, onPause, onStop and onDestroy

One can simple call the extension function:

  1. val onStopDisposer: Disposer = lifecycle.disposers.onStop

Much more shorter and convenient extensions are also offered, like for LifecycleOwner:

  1. class MyCoolComponent: LifecycleOwner {
  2. // ...
  3. private val onStopDisposer: Disposer = this.onStop
  4. }

Which leads to very concise and readable API’s inside your Activity or Fragment classes:


Example:
  1. class MyCoolFragment {
  2. // awesome other code
  3. fun onStart(){
  4. super.onStart()
  5. awesomDataProvider.query()
  6. .flatMap(::pepareForAwesomeness)
  7. .filter(::isAwesome)
  8. .subscribe(::displayAwesomeData)
  9. .disposeBy(onStop) // <--- Will automatically be disposed when onStop() is called.
  10. }
  11. }
Advanced: Upstream dispose

It is also possible to put the .disposeBy call before the .subscribe.
But be aware, that this will only dispose the upstream not the downstream, which is
often okay, but should only be used with caution!

  1. awesomDataProvider.query()
  2. .flatMap(::pepareForAwesomeness)
  3. .filter(::isAwesome)
  4. .disposeBy(onStop) // <--- Will dispose everything above it when .onStop() is called
  5. .subscribe(::displayAwesomeData)
Create you own Disposer

You can easily create your own Disposer by calling

  1. val disposer = Disposer.create()

Each call of

  1. disposer.dispose()

Will dispose all currently managed disposables and reset the Disposer

⚠️ Be aware: This behaviour differs from CompositeDisposable and actually
is more like CompositeDisposable.clear.