In any API centric native application like the Open Event organizer app (Github Repo), there is a need to access data through network, cache it for later use in a database, and retrieve data selectively from both sources, network and disk. Most of Android Applications use SQLite (including countless wrapper libraries on top of it) or Realm to manage their database, and Retrofit has become a de facto standard for consuming a REST API. But there is no standard way to manage the bridge between these two for smart data loading. Most applications directly make calls to DB or API service to selectively load their data and display it on the UI, but this breaks fluidity and cohesion between these two data sources. After all, both of these sources manage the same kind of data.
Suppose you wanted to load your data from a single source without having to worry from where it is coming, it’d require a smart model or repository which checks which kind of data you want to load, check if it is available in the DB, load it from there if it is. And if it not, call the API service to load the data and also save it in the DB there itself. These smart models are self contained, meaning they handle the loading logic and also handle edge cases and errors and take actions for themselves about storing and retrieving the data. This makes presentation or UI layer free of data aspects of the application and also remove unnecessary handling code unrelated to UI.
From the starting of Open Event Android Orga Application planning, we proposed to create an efficient MVP based design with clear separation of concerns. With use of RxJava, we have created a single source repository pattern, which automatically handles connection, reload and database and network management. This blog post will discuss the implementation of the AbstractObservableBuilder class which manages from which source to load the data intelligently
So, first, let’s discuss what features should our AbstractObservableBuilder class should have:
- Should take two inputs – disk source and network source
- Should handle force reload from server
- Should handle network connection logic
- Should load from disk observable if data is present
- Should load from network observable if data is not present is disk observable
This constitutes the most basic data operations done on any API based Android application. Now, let’s talk about the implementation
Since our class will be of generic type, we will used variable T to denote it. Firstly, we have 4 declarations globally
- UtilModel tells us if the device is connected to internet or not
- reload tells if the request should bypass database and fetch from network source only
- diskObservable is the database source of the item to be fetched
- networkObservable is the network source of the same item
Next is a very simple implementation of the builder pattern methods which will be used to set these variable fields
UtilModel is the required dependency, and so is added as a constructor parameter.
All right, all variables are set up, now we need to create the build function to actually create the observable:
First of all, we check if the caller forgot to add disk or network source and throw an exception if it is actually so. Next, we use defer operator to defer the call to getReloadCallable() so that this function is not executed until this observable is subscribed. Some articles over the internet directly use combine operators from Rx to make things easy, but this lazy calling is the most efficient way to do things because no actual call will be made to observables.
Secondly, you can easily test the behaviour in unit tests, by verifying that
- no call to the network observable was made if the data inside disk observable was present; or
- call to network observable was made even if there was data in disk observable if the reload request was made
These tests would not have been possible if we did not employ the lazy call technique because the calls to the observables and utilModel would have been made before the subscription to this model happen, in order to create this observable eagerly.
Now, let’s see what getReloadCallable does
This function’s role is to return the disk observable if the request is not a reload call, or else return an empty observable, so that force network request happens to reload the data
So it returns a Callable which encapsulates this logic, and besides that, it also adds a log if loading from disk about the type of item loaded and the thread it was loaded on
Connection and Database Switch Logic
In the next chain of operation, we make a switchIfEmpty call to getConnectionObservable(). Because of the above reloading logic, switchIfEmpty serves 2 purpose here, it changes to API call:
- if db does not contain data
- If it is a reload call
The observable we switch to is returned by getConnectionObservable() and its purpose is to check if the device is connected to the internet, and if it is, to forward the network request and if it is not, then return an Error Observable.
We use util model to determine if we are connected to internet and take action accordingly. As you can see, here too, we log about the data being loaded and the thread information.
Lastly, we want to ensure that all processing happens on correct threads, and for that, we call compose with an Observable Transformer to make all requests happen on I/O scheduler and the data is received on Android’s Main Thread
And that’s all it takes to create a reactive, generic and reusable data handler for disk and network based operations. In the repository pattern we have employed in the Open Event Android Orga Application, all our data switching and handling code is delegated to it, with unit tests and integration tests testing the individual and cross component working in all cases.
If you want to learn more about other implementations, you can read these articles
- Loading Reactive Stream data from Multiple Sources: http://blog.danlew.net/2015/06/22/loading-data-from-multiple-sources-with-rxjava/
- Database caching from network using Realm and RxJava: https:[email protected]/caching-with-realm-and-rxjava-80f48c5f5e37
- Cache first database and network implementation using Observable concat: https://medium.com/mobiwise-blog/load-cache-before-api-call-observable-concat-f527f267656