Skip to content

Instantly share code, notes, and snippets.

@photizzo
Last active April 23, 2019 15:39
Show Gist options
  • Save photizzo/e0143813170cd28b1da198181f4f030b to your computer and use it in GitHub Desktop.
Save photizzo/e0143813170cd28b1da198181f4f030b to your computer and use it in GitHub Desktop.
val cacheObservable:Observable<Data> = cacheSourceObservable
val remoteObservable:Observable<Data> = remoteSourceObservable.doOnNext { data ->
saveDataToCache(data)
}
val sourceObservable = Observable.concat(
cacheObservable.onErrorResumeNext(Function<Throwable, Observable<DriverStatsEntity>> { Observable.empty() /*ignore any errors from serving data from cache*/ }),
remoteObservable.onErrorResumeNext(Function<Throwable, Observable<DriverStatsEntity>> { throwable ->
if (!cache.hasCachedData()) Observable.error<Throwable>(throwable) /*show error from server since there is no data to be served*/
Observable.empty() /*ignore any errors from remote server and continue serving data from cache*/
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment