Posted on

Loading data from few sources by rxjava

Observable<Data> memory = ...;  
Observable<Data> disk = ...;  
Observable<Data> network = ...;
// Retrieve the first source with data
Observable<Data> source = Observable  
  .concat(memory, disk, network)
  .first();

Save data to cache

Observable<Data> networkWithSave = network.doOnNext(data -> {  
  saveToDisk(data);
  cacheInMemory(data);
});
Observable<Data> diskWithCache = disk.doOnNext(data -> {  
  cacheInMemory(data);
});

Update data

Observable<Data> source = Observable  
  .concat(memory, diskWithCache, networkWithSave)
  .first(data -> data.isUpToDate());
Leave a Reply

Your email address will not be published. Required fields are marked *