Last active
February 23, 2018 10:14
-
-
Save baneizalfe/b1b1a70927d093a0d2e8c3423ba8332c to your computer and use it in GitHub Desktop.
Handling error in Observable|Flowable that is wrapped in LiveData
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
class LiveDataResult<T>(val data: T?, val error: Throwable?) | |
class LiveObservableData<T>(private val observable: Observable<T>) : LiveData<LiveDataResult<T>>() { | |
private var disposable = CompositeDisposable() | |
override fun onActive() { | |
super.onActive() | |
disposable.add(observable.subscribe({ | |
postValue(LiveDataResult(it, null)) | |
}, { | |
postValue(LiveDataResult(null, it)) | |
})) | |
} | |
override fun onInactive() { | |
super.onInactive() | |
disposable.clear() | |
} | |
} | |
class LiveFlowableData<T>(private val observable: Flowable<T>) : LiveData<LiveDataResult<T>>() { | |
private var disposable = CompositeDisposable() | |
override fun onActive() { | |
super.onActive() | |
disposable.add(observable.subscribe({ | |
postValue(LiveDataResult(it, null)) | |
}, { | |
postValue(LiveDataResult(null, it)) | |
})) | |
} | |
override fun onInactive() { | |
super.onInactive() | |
disposable.clear() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment