Android - Offline mode - Syncing data

Ahmed Wajieh picture Ahmed Wajieh · Apr 26, 2018 · Viewed 7.5k times · Source

I am working on android application which is driver application. The main of the driver application is to deliver the orders for the a particular client.

The driver will received a bulk of orders and he can change the status of each one e.g. (in_transit, on hold, delivered, canceled, rejected). Once the internet connection is dropped the driver is blocked from any action.

My goal is how can i make the application running in offline mode, once the internet connection is dropped the driver is still can take an action of each order and he can change the status locally for each one from new to in_transit to deliver and so one. And once the internet connection is back i can sync/push the data to server.

So my questions are: - How can i do that? - What should i saved to sync the api requests or order status?

Thanks,

Answer

Atish Agrawal picture Atish Agrawal · Apr 26, 2018

You can follow these steps in your app:

  1. Create a Local Database with a schema that holds the information regarding the stops and items.
  2. In the beginning, when a driver is starting his journey, he must be having the Internet. Fetch all the deliveries information and save it to the local database for use.
  3. Show this list to the driver from your local database.
  4. You can have 2 Flags named delivery_status and synced. Initially, delivery_status can be in_transit and the synced flag must be false.
  5. Create a background service that keeps checking for Internet Connection at a certain interval. If there is no internet, the service stops and nothing happens. But, if there is an Internet, fetch all the unsynced **(synced = false) ** data from the local database, and send the data to the server. After a successful response from the server, mark this synced flag to true. Repeat this step at every certain interval to check for unsynced data and keep sending this to the web server until all synced flags are true.

This way, you can make the driver deliver the items without having internet at times, and as soon as the internet is available, your background service will upload the status to the server.