Ahmed Wajieh
Ahmed Wajieh

Reputation: 89

Android - Offline mode - Syncing data

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,

Upvotes: 3

Views: 11158

Answers (5)

GParekar
GParekar

Reputation: 1219

First fetch all orders from server and saved to local database for local database use SQLite

Driver update the status locally i.e in SQLite local database.

Use an background service to check internet connection is available for each 15 min.

Make one status table in local db , If internet connection is available, send data to server and change status in local created table as a true by default it is false

Next time send only data has status false in local db.

Upvotes: 0

Atish Agrawal
Atish Agrawal

Reputation: 2877

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.

Upvotes: 10

Mayur Raval
Mayur Raval

Reputation: 3275

You can also use Couch base db. sync data to the server through sync gate way

Upvotes: 1

bremen_matt
bremen_matt

Reputation: 7367

The issue is that you need to save the data locally so that it can persist until you get the chance to send it to the server.

Your first decision is what behavior you want if the app is closed. If you want it do be saved even if the user closes the app, then you will need to write that data to disk. You can do that by writing to file (with a json), saving the data in SharedPreferences (if you only have a small amount of data), or by using a database. Databases are the most common approach if you will have a lot of data.

As far as databases go, Android natively supports SQL lite, but that may be difficult for a beginner. I would suggest looking into Realm, which I find very intuitive to use.

If you don't mind the data being forgotten if the user closes the app, then you could just put the data into one of the numerous Java collections, like an ArrayList, LinkedList, or Stack until the connection is restored. However, like I said, you run the risk of losing this data if the app is closed before the data is sent.

Upvotes: 1

jase
jase

Reputation: 237

Save the order(s) status first in a SQLite database. Then once an internet connection is attained, try to sync all the data.

Upvotes: 3

Related Questions