Design offline android apps
Recently, I worked on android app, where we need to provide offline support i.e. app should be able to show content even if there is no network connection available on device . When I worked on this application I considered following aspects.
App Architecture : I used combination of MVP , MVVM architecture in my application. Whenever user click on any item or refresh list of data items then request is handled by presenter . If required presenter can communicate with data repository and provide updated model to view, so that view can be updated.
Whenever there is network call in app , app first check in database for data and if required data is available then updated model is return back to view through presenter also view is binded with data at same time. Now if required data is not available in database then network call initiated by app and data saved in database. After this data flow will be same as in the case when required data was present in database.
To get refreshed data from server we used polling approach. We have Job scheduler in our app, which periodically checks server for data updates and if there is change in data on server , app download data and store it in database. As I mentioned view is binded with data present in database, so whenever changes occurs in binded data, view automatically gets updated.
Sync/Download: There are two approaches, which we can use to download/sync data from server. first is Push approach and second is Pull approach.
Push approach: In this approach whenever data changed on server, server send push notification to client and client sync/download data from server.
Pull approach: In this approach client periodically poll to server and if there is change in the data then it will be synced/downloaded by client. I used this approach in our application, because there was no push notification available on our server. Now question comes how client will know that there is data change on server. We can use multiple strategy like maintaining db version , whenever there is change in data on server , server increment db version and when client do polling client send it’s local db version to server and then server compare these db version and return appropriate response to client i.e. changed data or no data. Last synced/download time stamp of client can also be used by server to decide whether client need updated data or not.
IMHO Push approach is better than Pull approach, because it avoid frequent polling on server and save device battery.
Conflict Resolution : If client need to just download data from server then client replace old data with new data in device db, whenever there is data change on server. In case of sync more complex resolution techniques are required like client win approach , server win approach.
Job Scheduler : We need Job scheduler to periodically poll server and download changed data from server. Android has JobScheduler, AlarmManager, GcmNetworkManager classes for this purpose , but each one has some limitation depending on OS version of android device. I used evernote android job scheduler library for this purpose. This library automatically switch JobScheduler, AlarmManager, GcmNetworkManager depending on device OS version to schedule sync/download Jobs.
Database : It is important that when you choose database for offline app you should consider those databases which are efficient, have UI construct which automatically gets updated whenever there is updation in underlying database, support sync/download. I consider two databases Couchbase Lite , Realm Database and finally used Realm Database.
UI updation: In our application I have to show lists of data. So I used android RecyclerView and RealmRecyclerViewAdapter to show list in our application. RealmRecyclerViewAdapter update RecyclerView automatically whenever data binded with RealmRecyclerViewAdapter changes in Realm Database.
That’s all for Today. If you like this story please click on like and share button. Suggestions and comments also most welcome.