Garima Jain

Nov 17, 2016 10:57:54 AM

RXify.png

By - @ragdroid (Twitter, Medium)

We all have heard about the classic “Cache-First then Network” problem. If not, then let’s discuss about it a bit. Cache-First then Network problem states that:

 

“If the data is found in the cache return data, otherwise hit the server API to get data from the network.”

Cache-then-network Cache-Then-Network

 

This article briefly mentions how we can solve the above problem using RxJava and then describes another problem i.e. “Anti Cache-then-Network” OR “Network-then-Cache” problem. 

Rxify: Cache-then-Network

We will use the concat() operator to solve this problem. Let’s look at the code :

 
  Observable<List> getLectures() {
      Observable<List> cacheObservable = getLecturesFromCache();
      Observable<List> lecturesFromServer = getLecturesFromServer();
      return cacheObservable.concatWith(lecturesFromServer)
              .take(1);
  }
  

Rxify : Network-then-Cache

We often come across the case when we need latest data from server every time. And also if the data is not available due to network issues, we would rather have stale data than no-data. Let's state the Network-then-cache problem now :

“If network is available, fetch and return latest data otherwise access the cache to get saved data”

Network-Then-Cache Network-Then-Cache

 

Let us take an example, we want to fetch start-up details for our app. We want these details to be frequently updated and if we face any network issues, we will revert to fetching the details previously stored in cache. 

Can we use the concat() and take(1) sequence to solve this problem? 

no source

The answer is NO! 

The reason is that any networking library will throw an error in the case of network issues and therefore, instead of subscribing to our second observable, onError() would be called. Thus, we require some kind of error handling technique here.

Error Handling

One of the error handling approaches is mentioned below : 

Catch

recover from an onError notification by continuing the sequence without error

We can use the operators like onErrorResumeNext() or onErrorReturn() to solve our problem as follows :

 
service.startupDetails(request)
   .doOnNext(new Action1() {
     @Override
     public void call(StartupDetails startupDetails) {
//save details in cache startupStore.putStartupDetails(startupDetails); } }) .onErrorReturn(new Function<Throwable, StartupDetails>() { @Override public StartupDetails apply(Throwable throwable) { //fetching startup details failed, will return cached value" return getStartupDetailsFromCache(); } });

Whenever we will face any network issues, we will return the cached value. This approach of Network-then-cache is slower as we wait for the network call to complete before performing any task, but sometimes we need fresh data frequently and in those cases the above approach works great!

Problem Solved :)

dancing_android

 

All the best!!

 

Related Search Term(s): Influencer Tutorials, RxJava

Create, Design, Develop and Connect at AnDevCon D.C. 2017!

Thoughts? Leave a comment: