Sync device data with web-server - java

I have data in my application which must be updated on the server on the basis of network availability. I store this data on a local database on android using sqlite. Currently the idea is:
Data should be inserted on the local sqlite database
A service should be waiting as soon as the data is inserted it checks the network availability and send it to the server and wait for success response.
If the response true is received from the server it should update the sync status of that sqlite row and move to the next row to send the data.
I wanted to find out the best way to queue this data to send it to the server, additionally this queue should stop sending data to the server when the network is unavailable. Since there are lot of queues available in java which queu is the best to hold this data. Since the data can grow to any infinite size. It is a like a Producer-Consumer problem but what is the most efficient way to address it.
Thanks in advance.

Why would you store this data in a queue when it is already stored in a local database? I mean what happens if your application is shut down then the data in the queue is lost. Just write a service that checks the local database for new entries. If there are new entries try to push them to the webserver.

Related

Android: Linking and syncing Room database to online server database

I am developing an app that uses a database to store the data on the server, but I am trying to save some of the data locally, and in the event of no internet connection being established, save new data locally to the device and synchronize any changes to the server when an connection is re-established. What is the best and most efficient way to do this?
I have been looking at Androids Room persistence library and it seems like the logical choice, but I am not sure how it goes about synchronizing changes to/from the local storage database. I have looked at multiple threads and forums for help, but have had no luck so far. Please help.
One way is to build your own sync adapter: https://developer.android.com/training/sync-adapters
You will need to handle most of the sync logic between the client and the server, but that allows you to use any database technology in the server. From the docs:
A sync adapter doesn't automate any data transfer tasks. If you want to download data from a server and store it in a content provider, you have to provide the code that requests the data, downloads it, and inserts it in the provider. Similarly, if you want to send data to a server, you have to read it from a file, database, or provider, and send the necessary upload request. You also have to handle network errors that occur while your data transfer is running.
A sync adapter doesn't automatically handle conflicts between data on the server and data on the device. Also, it doesn't automatically detect if the data on the server is newer than the data on the device, or vice versa. Instead, you have to provide your own algorithms for handling this situation.
Use firestore and enable offline data persistence. https://firebase.google.com/docs/firestore/manage-data/enable-offline

Fetching data from another device

I'm working on an shopping list app where all the family can connect to the same list.
How can i make and save the list on one device and then connect from all the devices to the list?
What you need is the concept of Websockets.
WebSockets represent a long awaited evolution in client/server web technology. They allow a long-held single TCP socket connection to be established between the client and server which allows for bi-directional, full duplex, messages to be instantly distributed with little overhead resulting in a very low latency connection.
Thats means if you create a server with a web socket connection, and you allow other clients(android) to connect to that connection. You can send messages back and forth and every device connected will recieve it. All connected is how i like to call it. Think socket.io and node.js.
You can't fetch the device data without having a medium or network in between them and if you choose Bluetooth or wifi then your data will be local and other family member living another city won't be able to contribute. so at last if i'm guessing right you need a synchronised database which can give you realtime updates from another user.
This is where Firebase Realtime database comes in. you can use it in your project, it's easy and takes few minutes to configure.
since all the user will have their own app they can contribute to same database and others can see it instantly.
Read the docs here for android

How do I know when my firebase client loses connection to the firebase server?

I see that there's an onDisconnect() firebase method, however it looks like that method is intended to update other client devices that the current client device has gone offline. For me this isn't very useful. All of our API calls are coming from a central server which handles updates to our other client devices.
Basically, I only have 1 client device communicating with the firebase server... ever. So my question is, when that single device loses connection how will I know? This is probably the most frustrating part of all, Not only do I not know when I lose connection, but I'm still making calls as if each command is successful.
Offline Behavior
Every client sharing a Firebase maintains its own internal version of any active data. When data is updated or saved, it is written to this local version of the Firebase. The Firebase client then synchronizes that data with the Firebase servers and with other clients on a 'best-effort' basis.
As a result, all writes to Firebase will trigger local events immediately, before any data has even been written to the server. This means the app will remain responsive regardless of network latency or Internet connectivity.
Once connectivity is reestablished, we'll receive the appropriate set of events so that the client "catches up" with the current server state, without having to write any custom code.
Take a look at Detecting Connection State in the Firebase Guide. It describes the magic data location /.info/connected that you can monitor for changes in the client's authentication state.
Note: For version 3.0+ see onDisconnect

Send notification to the client from a web server

My setup consists of a Weblogic server that runs various REST services that accepts an HTTP request from a client(an android app), reads data from a MySql database and sends this data as a response to the client.
My requirement is that the client should be notified as soon as the database is updated. I have implemented a "polling" mechanism. The client constantly sends requests and checks for any changes made in the database. However this is not a very efficient mechanism.
Can you please suggest any efficient ways of achieving this?
Thanks in advance!
One of the way to use writing a CRON job for observation of Database change.
also, SO has already discussed this topic. refer below link,
Oracle database to send notification / Invoke a java method on reaching Time specified in a column

Dealing with lost connection insert

I have a SQLite database which will store all the data entry on an Android application. It will then check if it has a Network Connection - If it does it will send a JSON post to a Restful web service.
I was going to do it straight to the web service and then save it in the SQLite database if there was no connection , but what if connection is lost half way through the transfer?
What should I do if I lose Network Connection? My suggestion on this would be the following
Insert data into SQLite Database to store data
Check the connection every 5mins. (What if the user gets a connection in between, and then inserts the data?)
If they have a connection - get the SQLite database results, check them against the MySQL database, and insert any fields that do not exists? (Maybe put a flag in the database to see if it has been posted)
Is this a good way of doing it? Or am I thinking about this the wrong way?
Thanks
James
Consider the use of some sort of queue in your Android application which will hold requests against your web service until they are completed successfully.
http://en.wikipedia.org/wiki/Queue_(abstract_data_type)

Categories