I am using RxJava in a MVP solution and I want to achieve this scenario:
try to get the server data and if its successful populate the view using server data
if its unsuccessful for any reason (no internet - server unreachable - server internal error) show the appropriate message but also populate view with the cached data.
constraints:
I don't wanna use any extra callback (RX can do it all)
I don't wanna access local repo directly from Presenter
what I tried:
in my Repo:
override fun getRepos(userName: String, page: Int, pageSize: Int):Observable<List<Repo>> {
return githubRemoteService.getReposList(userName, page, pageSize)
.subscribeOn(schedulersProvider.ioThread())
.flatMap { repos ->
val mappedRepos = remoteResponseMapper.mapRepoResponse(repos)
githubLocalService.saveRepos(mappedRepos)
Observable.just(mappedRepos)
}
.onErrorResumeNext(Observable.just(githubLocalService.getReposList(userName, page, pageSize)))
.observeOn(schedulersProvider.mainThread())
}
in my presenter:
githubInteractor.getRepos(userName, page, pageSize).subscribe(
{ repos ->
showReposInView(repos)
},
{ error ->
digestAndShowErrorInView(error) //the problem is here - no error will be present here
})
as we know when I use onErrorResumeNext
, the observable source changes but the error will never be emitted.
how can I emit the error first and then emit local repo data ?
if it cant be done like this, how can I change my scenario to get the same scenario ?