Dealing with lost connection insert - java

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)

Related

Update local DB only when there is a change in remote DB (Android Studio)

I'm using OkHttpClient to fetch data from remote DB and display the response to the user.
The user will never make changes to the remote DB. I'm now implementing SQL lite local DB, since I don't think it's good to use APIs to fetch the same data all the time (since the user will send requests frequently and the data change in remote DB will be minimal). My question is how can I update my local DB when remote DB updates/changes.
I have read that updating local DB periodically is bad practice. so, what's the best way to do this?
You have no way to spy on the remote DB "unless you make API calls on a regular basis you need to sample it every few minutes to truly know if it has changed" OR "you assume that every time you use the app your remote DB changed and sync it" if even this is not a viable solution for you and you need it as it updates. You need to use a Realtime DB

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

SQLite functions at android apps

I'm very new at programming. I want to make an app where,
1. User1 will make an order (log in as user_profile_1)
2. User2 will work with that order (log in as user_profile_2)
One user shouldn't have access to other's data.
Will SQLite allow me to do that? Seems, database must not run on user's device. And database will always get new records, so we need to update it all the time.
For this case, you have to create a server to store data, I think you should do these things:
Use PHP, NodeJS or some languages server like this to create APIs.
Use a MySQL database or some other database to store data on your server.
Then, different users can log in on their device and you will call your APIs to show what you want.
If you use a SQLite database, the db will be local in the device. The records in the database do not share with other devices.
If you want to access the database from different devices the database will need to exist in a remote server.
You can do differents things:
You can use a database MySql in a remote server with allowed remote access.
You can create a rest api in a LAMP server. You can do this for example with PHP and MySql. You create a GET/POST web page, them pass the data, and the php save the information in the database. You can get the information from the database in the same way, but you should ask to the db in a periodicly time

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

Sync device data with web-server

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.

Categories