Reputation: 890
In my application I am getching data from api and if there is an error while doing that I would like to get data from database but also show error message on the screen.
So how I thought about doing this is use something like onErrorResumeNext
. I can get data from database using this but the problem is that onError
is not called then and I can't show error message to user. So my code is:
override fun getData(): Observable<List<MyData>> {
return getDataFromApi().onErrorResumeNext(getDataFromDatabase())
}
How can I somehow fetch database on error and simultaneously know that error message needs to be displayed?
Upvotes: 1
Views: 160
Reputation: 21407
You could change the way you model the domain to something like the following:
sealed class Result {
class Simple(data: List<MyData>) : Result()
class HasError(data: List<MyData>, throwable: Throwable) : Result()
}
Then you can do:
fun getResult(): Observable<Result> {
return getDataFromApi().map<Result> { Result.Simple(it) }
.onErrorResumeNext { throwable ->
getDataFromDatabase().map { myDataList ->
Result.HasError(
myDataList,
throwable
)
}
}
}
Upvotes: 1