How to validate Offline cached data and data coming from API - android

I am trying to make an app in which I am going to use room database for offline data caching and using NodeJs and MongoDB as a backend service.
What I am going to do is when app first opens it fetches data from server and stores in room database from where it shows in database.
My problem is whenever some new data is updated on server how would I know whether it is available in room database and when to fire server request.
Someone please let me know how can achieve this any help would be appreciated.
THANKS

You are going to need to implement some sort of routine where you can check and validate (update) the data. However, this will depend on the importance of the data and rather if it should be updated and how often.
These are some of the solutions you can look at:
Long/short polling - Client Pull
WebSockets - Server Push
Server-Sent Events - Server Push
I would probably use some sort of real-time communication (web sockets) or use a real-time database to notify the app of changes when something needs to get updated. That said it would also depend on the technology, for instance, Firebase already offers offline caching.

You can achieve this functionality in multiple ways
Configure firebase notification in your app, whenever any server side data update happen just trigger a notification, based on that you can call api and store data in room.
Maintain some version code related to updated server side data in your api, based on that version code you can write logic for storing data in room.

Related

How design android app and server which provide work offline as well online

I need develop an app which show rates of commodities and that is going to used in very low network bandwidth areas. So I need to download data whenever network is available/when user allow network use through app switch, but user should be able to see whatever data is downloaded in the app. So I am searching for good client/server design which support my requirement.
I thought one approach where server send data as per the client request and in the request client will send number of id it wanted fetch with from and start point, but unfortunately currently server guys unable to do that.
So looking for alternate approaches.
Any article , Any sample will help here.
Save data in local database (SQLite) and display when it is in online and display data from SQLite when it in offline
Have a look at Realm. Realm provides a very elegant client/server database sync and best of all its free.

Syncing SQLite database with remote mysql data in android

How to synchronise SQLite database and remote MySQL data in android?
To make it clear, I want to know how syncing between sqlite data and remote mysql works .. i.e., when the remote mySQL database is updated, SQLite data should also be updated without any button press or scroll down? How can I implement that using Android Studio and Wamp server ?
Thanks in advance!
I guess you can use broadcast receiver inside a service of your android application. If not you can write a service and inside the service you can use a timer to send http request to the server and keep syncing. To get a knowledege about syncing a SQlite database with remote mysql database refer to this tutorial and if you need it to happen without a button press then implement the code in the following tutorial inside a service and remember to use a timer.
TUTORIAL LINK
I don't know any way to make a mirror of your mysql database on your android's sqlite database. There are two ways I can think off when it comes to synchronizing data on your phone. The first one is by using a syncadapter. But this will only pull the data from the server and so you won't have your synchronization as soon as an update is done.
The second one is by using push notifications. With FCM, firebase cloud messaging, or GCM, google cloud messaging, you can tell your server to send a message to every phone that is connected to it. Your phone can then handle the message and synchronize itself with your database. Obvisouly in either solutions you won't have a direct access to your database as it brings huge security problems so you'll need to pass by a web server with an API to connect your application to your database.
Here are some useful links about FCM, Syncadapters, etc

How to synchronize SQLite and MySQL databases using push notifications?

I have an SQLite database on Android and a MySQL database on a server. I want to synchronize these databases when a user edits data on their phone or edits data on a website.
I know how to update the MySQL database on the server when a user makes changes on their phone but I don't know how to update the Android database when a user makes changes on the website.
I have read into push notification and believe this to be a good path to follow but I have a few questions about it:
When a user updates data through a website it will send a push notification to that user's phone saying changes have been made. Can this push notification trigger to update the Android's database with the new changes made on the Server database?
What if a user turns off push notifications? Will I still be able to trigger for their Android database to be updated?
I have also read up on SQLite and MySQL database synchronization and found this post SQLite and MySQL sync but did not find the post helpful for my situation.
Are push notifications a good way to go or should I be using a different approach?
In a nutshell - I want a way for the Android device to detect changes on the MySQL database and update its SQLite database without the user initiating the synchronization.
I'm afraid I've not used push notifications. But a solution might be: You could create an early method call to an Asynchronous polling event from the launcher onCreate() that looks up the server to see if any changes have been registered (though an API of some sort) in the MySQL, and then update the SQLite that way? Since it's the first thing that happens on launch, technically the user isn't initiating it. Granted this won't update during use of the app, unless you repeat poll at regular intervals?
Token based pagination approach.
Assumptions: or calls you need to take
One of the databases will the source of truth, in case of differences in the two, which data is true, and which will be overwritten? - assuming remote database is source of truth
What's the frequency of data changes? - assuming its not realtime critical
How much stale data are we OK with dealing on the app. - assuming we're OK with a few minutes of difference
How to ensure data is consistent
Find a method to associate a token, which can be used to identify till which record is the data in sync. This is important no matter how assured you are of web requests, they will fail. So the best method is to send the last token that have stored, and the web endpoint will return data from that token to the latest value.
If the volume of data is too much here, sending chunks > sending all of it. Chunks work the same way, based on tokens.
These tokens can be simple PK auto increment column as well.
How to deal with time difference, stale data
If your application demands some data to be near realtime, better to categorize data based on a fiew screens, and whenever the user comes to the said screen, send a request in background to fetch related data columns only. Will ensure the data stays in sync w.r.t to the important columns. This is a classic push/pull approach. Can also be done on the splash screen itself.
as a rule of thumb, if you need something urgent, pull.
if it can wait, wait for a push.
Push notifications are OK as far as:
they should be silent.
there'a a limit on the number of push notifications that you can send
have costs associated
what's the fail - check mechanism? What if the requests fail?

Sync data between client and server

I have mobile app. Something like to do list or calendar. Teoretically user can have a few devices with that application on a defferent platforms and so on. I would like to create a automatic synchronization between them through a own server. What is the best practice: update all the information or only the changes? On the one hand usually there is no a lot of data when it's about a to do list but who knows?
The correct approach is not date/time as others suggest, as time can go out of sync. The right algorithm is to keep the checksum of the data entries during last synchronization. On next synchronization you compare current checksums with stored ones, then you know whether the entry has been changed on the server, on the client or both.
Our open-source Rethync SDK lets you implement the above approach quite easily and is available for Android (not for iOS at the moment).
I am doing something similar in my application. I have a last modified date field with each entity that I need to sync. Then periodically, I post this data to the server (actual data + date and time). Now the server can do one of two things. It will check the corresponding data on server side and compare the last modified date. If what the server is latest, it will return the latest data in response. If not, it will update its data and send a response indicating what client has is latest.
Of course you can do several optimization. That is, mark the data as "dirty" so you know whether to even send your data to server. If the phone does not have modified data, your sync is basically getting the latest data from server.
Basically server does the heavy lifting and does all the logic necessary to maintain the latest data on its end and send responses to client appropriately.
Good Luck
Best approach is use a time stamp to handle this.
Initial request to server with time stamp value 0.
Server will give the all the data first time with Time-stamp.
Store the Time stamp to sharedpreferences.
In All next request pass the time stamp back to the server
Server will send only those data which are add/update/ after that
given time stamp
That is it.
There is a new alternative to the syncing problem. It's called EnduroSync from Orando Labs. You can sync object data stores between devices on Android and iOS now, with others coming soon.
Full Disclosure: I work for Orando Labs.
The EnduroSync clients allow you to create object data stores on the local devices. The clients are fairly sophisticated - data is modeled as native objects for each client we support (iOS and Android now, more coming). The clients work offline and online. The data is saved to an sqlite database locally.
As you change objects in your model, the deltas are recorded on the device. At some point, you can 'sync' the object data store. Syncing uses a commit/push/pull process (like git), but this is invisible to you. The sync brings your local copy up to date with whatever is on the server, and sends up any changes you have made. Conflicts are resolved using a timestamp based merge, so newer data is not overwritten by older data.
EnduroSync is an online service, so there is no server setup on your end.
There is also a flexible permission system which lets you share the object data stores in a variety of ways. For instance, most applications will have one or more object data stores for each user, for preferences, notes, tags, etc. You can also share object data stores per app, per user type, and with wild cards, many other ways.
So basically you use our client SDK's to model your data on the device. Modeling is with simple objects in the native programming language of the device. If you sign up for the syncing service, you get the syncing also.
Here is another approach.
Issue :I need to have the appointments of doctors syned to client (mobile device) from the server. Now the appointments can drop off or the data could possibly change on the server. Having the client to know what change and sending a request back to server could be an expensive service.
Possible approach : Have the server do the heavy lifting. Keep a table which stores values of time stamp and if a change happened with regard to an appointment - cancellation / reschedule etc. The client would then look at this table to see if anything changed. In reality we don't need to sync anything but only the delta which server can provide to the client based on what it has and what is at Client. There is one aspect which needs to be taken care of is updation of info from client to server and traditional conflict management can be done where client can update the server when a data connectivity between client and server exists.
Essentially the approach is to have only the deltas synced by maintaining a checksum or data change log to PUSH changes to the client.

Sync SQLite Database to App Engine Datastore?

The android app stores the data in SQLite database during the offline mode. When online I want the app to sync (in both direction) with the datastore(database) in cloud server (App Engine). How do I implement this functionality, so that I can show the data captured on phone on a web application. Also please suggest any simple alternative way if any..
I just write out my data as a String (using a format I can reconstruct my data with), pass that to AE, parse it and store/display it.
You could use json too.
or try http://mylifewithandroid.blogspot.jp/2010/10/client-server-communication-with-json.html
for the sync part I use a timestamp. If the timestamped result isn't recorded on the server, I record it. I send back the recorded stamps to the client and delete them from the store. Of course the server can also send back new results if a user's records were updated from a different client.
In general, you should implement some complex algorithm, that will be doing synchronization depending on your needs, and then make it in code on both side (server and client). This not quite a simple task, in general. Useful keywords for googling: SOAP, REST, JSON, ...

Categories

Resources