I\'ve been using alot of RxJava Observables
converted to LiveData
in my code using LiveDataReactiveStreams.fromPublisher()
library. So I t
Your solution is fine if you want to use LiveData and Rx together.
If you just want to auto-dispose your subscription you could implement it on Disposable like this:
private class LifecycleDisposable(obj: Disposable) :
DefaultLifecycleObserver, Disposable by obj {
override fun onStop(owner: LifecycleOwner) {
if (!isDisposed) {
dispose()
}
}
}
fun Disposable.attachToLifecycle(owner: LifecycleOwner) {
owner.lifecycle.addObserver(LifecycleDisposable(this))
}
and call it like
Observable.just(1, 2, 3).subscribe().attachToLifecycle(this)
where this
references any LifecycleOwner.