Skip to content

Instantly share code, notes, and snippets.

@miroslavign
Created March 13, 2017 18:27
Show Gist options
  • Save miroslavign/f75a10d50cfb55dd97555a533779abf8 to your computer and use it in GitHub Desktop.
Save miroslavign/f75a10d50cfb55dd97555a533779abf8 to your computer and use it in GitHub Desktop.
All RX android related
from
http://blog.danlew.net/2015/06/22/loading-data-from-multiple-sources-with-rxjava/
https://github.com/dlew/rxjava-multiple-sources-sample/tree/master/src/main/java/net/danlew/sample
// Our sources (left as an exercise for the reader)
Observable<Data> memory = ...;
Observable<Data> disk = ...;
Observable<Data> network = ...;
// Retrieve the first source with data
Observable<Data> source = Observable
.concat(memory, disk, network)
.first();
The difference between the two calls is that first() will throw a NoSuchElementException
if none of the sources emits valid data, whereas takeFirst() will simply complete without
exception.
// save/cache data
Observable<Data> networkWithSave = network.doOnNext(data -> {
saveToDisk(data);
cacheInMemory(data);
});
Observable<Data> diskWithCache = disk.doOnNext(data -> {
cacheInMemory(data);
});
// kick off stale data
Observable<Data> source = Observable
.concat(memory, diskWithCache, networkWithSave)
.first(data -> data.isUpToDate());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment