I\'m in search for a better way to achieve a simple Observable fallback system for empty results when using RxJava. The idea is that, if a local query for a set of data results
Another variant. In my case I need to try one cached entry first, then lazily attempt the other if not present:
public void test() {
String cacheRowKey = "row";
Observable = cacheLookup(cacheRowKey, "newVersion").switchIfEmpty(
Observable.defer(() -> cacheLookup(cacheRowKey, "legacyVersion").switchIfEmpty(
Observable.defer(() -> onMiss(cacheRowKey;
}
private Observable cacheLookup(String key, String version) {
// Delegate to the real cache - will return effectively
// Observable.just(xxx) or Observable.empty()
...
}
private Observable onMiss(String key) {
// do the expensive lookup to then populate the cache
...
}
All of which are lazily done on demand.