Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to AutoDispose

Avatar for Shohei Kawano Shohei Kawano
January 24, 2018

Introduction to AutoDispose

If you use RxLifecycle, maybe now is the time to switch from it to AutoDispose.

"AutoDispose is an RxJava 2 tool for automatically binding the execution of RxJava 2 streams to a provided scope via disposal/cancellation."

- https://github.com/uber/AutoDispose
- http://blog.danlew.net/2017/08/02/why-not-rxlifecycle

Avatar for Shohei Kawano

Shohei Kawano

January 24, 2018
Tweet

More Decks by Shohei Kawano

Other Decks in Technology

Transcript

  1. https://github.com/uber/AutoDispose#autodispose AutoDispose • From Uber Technologies • “RxJava 2 tool

    for automatically binding the execution of RxJava 2 streams to a provided scope“ • Architecturally better version of RxLifecycle
  2. CompositeDisposable private val disposables = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) ... myObservable.doStuff() .subscribeBy { ... } .addTo(disposables) } override fun onDestroy() { disposables.dispose() super.onDestroy() }
  3. CompositeDisposable private val disposables = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) ... myObservable.doStuff() .subscribeBy { ... } .addTo(disposables) } override fun onDestroy() { disposables.dispose() super.onDestroy() }
  4. CompositeDisposable private val disposables = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) ... myObservable.doStuff() .subscribeBy { ... } .addTo(disposables) } override fun onDestroy() { disposables.dispose() super.onDestroy() }
  5. CompositeDisposable private val disposables = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) ... myObservable.doStuff() .subscribeBy { ... } .addTo(disposables) } override fun onDestroy() { disposables.dispose() super.onDestroy() }
  6. Extension Functions @CheckReturnValue inline fun LifecycleOwner.scope(): LifecycleScopeProvider<*> = AndroidLifecycleScopeProvider.from(this) @CheckReturnValue

    inline fun LifecycleOwner.scope(untilEvent: Lifecycle.Event): LifecycleScopeProvider<*> = AndroidLifecycleScopeProvider.from(this, untilEvent) // autodispose-android-archcomponents-kotlin
  7. AutoDispose with Arch Lifecycle protected val scope: LifecycleScopeProvider<Lifecycle.Event> by lazy

    { AndroidLifecycleScopeProvider.from(this) // or scope() }) … store.myState.autoDisposable(scope).subscribeBy(onNext = { … }) // Activity.kt
  8. AutoDispose with Arch Lifecycle protected val scope: LifecycleScopeProvider<Lifecycle.Event> by lazy

    { AndroidLifecycleScopeProvider.from(this) // or scope() }) … store.myState.autoDisposable(scope).subscribeBy(onNext = { … }) // Activity.kt