Created
December 14, 2018 19:09
-
-
Save afollestad/4a8b08b90a30b3445222c7fce38f4902 to your computer and use it in GitHub Desktop.
Make an Observable Lifecycle aware - disposing itself when the lifecycle stops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @author Aidan Follestad (afollestad) */ | |
class LifecycleAwareDisposable( | |
private val disposable: Disposable | |
) : LifecycleObserver { | |
@OnLifecycleEvent(ON_DESTROY) | |
fun dispose() = disposable.dispose() | |
} | |
/** @author Aidan Follestad (afollestad) */ | |
fun LifecycleOwner.ownRx(disposable: Disposable) { | |
this.lifecycle.addObserver(LifecycleAwareDisposable(disposable)) | |
} | |
/** @author Aidan Follestad (afollestad) */ | |
fun Disposable.attachLifecycle(lifecycleOwner: LifecycleOwner) { | |
lifecycleOwner.ownRx(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment