Anyone have any best idea to work Android application online as well as offline? Also, how can I manage server side data sync with the local database?
-
3See that's an interesting question because that's the problem every single client application and backend developer has to solve and needs to figure out in their API and their application design. Shame that *this question is too broad* and depends on what your database is on both client and server side, and what technologies you opt to use, and what your project limitations are etc etc. – EpicPandaForce Sep 15 '17 at 09:15
-
I would suggest, "Hire some one to do this". – Jaydipsinh Zala Sep 15 '17 at 09:15
-
The answer to the first question is written in your second question, `online as well as offline` by having a local database sync whenever the user goes online. The answer to the second question is a combination of services and AsyncTask 's – Yamen Nassif Sep 15 '17 at 09:16
-
@EpicPandaForce same reaction shame its too broad. – Yamen Nassif Sep 15 '17 at 09:16
-
The thing is, I have already connected my application online. But for offline features how much we can handle on our side? As I'll not be having any control on the data connectivity and user. – The90sArtist Sep 15 '17 at 09:18
-
It dependes on what data you would like to show in offline mode. – Sarthak Mittal Sep 15 '17 at 09:18
-
Whole application data. @Sarthak – The90sArtist Sep 15 '17 at 09:19
-
Depends on whether you allow operations when the user connectivity is not available, if you queue them, if there can be multiple concurrent operations if they are enqueued (in which case how you handle merge conflicts),e tc etc. hell if your backend server is a ROS then that changes the whole question, for example – EpicPandaForce Sep 15 '17 at 09:19
-
It depends on the application it self, take facebook as example it allows you to add comments offline then sync them online, and it also shows you latest fetched posts if you were offline, whatsapp is the same thing. but not youtube. – Yamen Nassif Sep 15 '17 at 09:19
-
But to upload all pending changed which are stored in local database while user was offline using retrofit will be difficult. Because every request will be async. – The90sArtist Sep 15 '17 at 09:20
-
@The90sArtist and thats the problem, handling everything is not easy. Async tasks with services and threads all together will be working to do the job – Yamen Nassif Sep 15 '17 at 09:21
-
Also, data sync have to be after every 25-30 mins interval. For this i am using repeating alarm. But even this wont work I guess. – The90sArtist Sep 15 '17 at 09:24
1 Answers
Steps to take for a complete offline application:
1) Your UI always displays the data from the db and actively listens to any db changes.
2) Whenever user interacts with your application, you directly update the db and mark the relevant fields in your db to sync with your server. (Note that your UI gets updated instantly as it is listening to db changes)
3) Your network layer listens to marked field changes in your db and tries to sync with server.
4) When server side changes are successful, you unmark your db fields which are now synced successfully.
5) If you are implementing a down-sync (i.e: server has the updated data and wants the client to update accordingly), you can send a push notification to the device and make a network call to fetch the latest data depending upon the notification message.
For this to work smoothly, you will need a reactive database. Realm database is one such example that I have used.

- 5,794
- 2
- 24
- 44
-
This is so easy if I am just dealing with some small data. What if data is large? I need to sync each data with updated data. – The90sArtist Sep 15 '17 at 09:29
-
I require more context to answer that, suppose you are making news app, you can fetch all data from server once a day (say) in the morning, and display data from db – Sarthak Mittal Sep 15 '17 at 09:33