Skip to content

Instantly share code, notes, and snippets.

@baneizalfe
Created February 23, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baneizalfe/95a26d89d47d14c066000803e463ea18 to your computer and use it in GitHub Desktop.
Save baneizalfe/95a26d89d47d14c066000803e463ea18 to your computer and use it in GitHub Desktop.
Handling error in Obserable
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