Created
February 23, 2018 10:09
-
-
Save baneizalfe/95a26d89d47d14c066000803e463ea18 to your computer and use it in GitHub Desktop.
Handling error in Obserable
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