Which is the best way to synchronize Android application data from server? - android

I have three informative screens like Facebook blogs in my application. Application works Online and Offline. User can see the data offline.the data is Cached in Database.
When data is added/updated/deleted to server from active users.
The problem is, I want to update all other active application users with new data without affecting their works and in short time. In short, want to update it in background. How I can achieve this?
I have tried below solutions but had some issues:
1. After registration fetching all data from server and store in Database for Offline use.
Problem with this solution: Always showing progress bar for caching data impact bad user experience. user will get irritated.
2. Implemented background processing GCM push notification.(Means without displaying push notification to user process it in background)
Problem with this solution: If user blocks notification then the application never get synchronized with new data added/updated/deleted on server.

Well, if the data is in Json, you can use Retrofit for data fetching, its amazingly fast.
After registration fetching all data from server and store in Database
for Offline use.
Solution: After the user is registered show the Progress for the first time while your getting the data and updating the database, after that display the data from database and keep updating the data in the background.
Or you can simply make a ProgressBar of height 2dp near ActionBar to show the user while data updates in the background and user can interact with the current data available from database.
You can use MaterialProgressBar or make your own.

Related

If firebase realtime database doesnt trigger data obtention unless there are modified data then how does offline database work?

or closing the app? How can I acess that data?
As I understood, firebase realtime database stores data and when a change in data is produced, you can retrieve the modified data. The app detects an event sent by the fb realtime database, and then downloads the new data. That is useful only with newly added data, but let's say you close the app. ¿Do you have the data from previous session? Lets set an example, an app like whatsapp. New messages are sent by an user to fb rt database, then firebase sends an event, the app detects the event, then downloads new data and displays it. But what if you close and reopen the app? Is the message still being displayed?
If not, how do I access that data? Should I keep data on a sqlite database on my own?
//Edit:
The way the app is intended to work is:
After data is modified, download new data. When app is closed after data has been downloaded, I want that data(and all previous data the app has downloaded) to still be there, so I can display the data when reopening the app. When app is closed, or there is no internet, then the app waits until there is internet and app is opened to download the new data. The part I do not understand yet is: Does the data downloaded from the server when there is new data disappear from the app after you close the app? Or it does disappear forever from the app memory if you do not save it in your own local database?
Do you have the data from previous session?
Absolutely. If you are using the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Your data will be available even if your devices goes offline.
But what if you close and reopen the app? Is the message still being displayed?
You get all the data from the "previous session".
When talking about listeners that are getting the data in realtime, please don't forget to remove them according to the life-cycle of your activity as explained in my answer from this post.
Here you can also find more informations about the case in which you don't remove the listener.
Edit:
When app is closed after data has been downloaded, I want that data(and all previous data the app has downloaded) to still be there
This will certainly happen.
When app is closed, or there is no internet, then the app waits until there is internet and app is opened to download the new data.
This is what is happening if you are enabling offline persistence, as explained above.
Does the data downloaded from the server when there is new data disappear from the app after you close the app?
No, it does not.
Or it does disappear forever from the app memory if you do not save it in your own local database?
Will not disappear. There is no need for any other databases.
When your application is in background (closed), it can't use registered listeners to maintain the synchronisation of your local data with the online data on firebase database.
When the user reopen the application, the data is automatically resynchronised to get the latest data from your database. See : https://firebase.google.com/docs/database/android/offline-capabilities
If you want to notify something to your user when the app is closed (like a new message or something), the best bet is to use Firebase Cloud Messaging : you can send notification messages that are displayed to your user (even if app is closed), or send data messages and determine completely what happens in your application code.

Efficent way of internet calls in android

My current android app is azure mobile app where a user can book any service.I need to show the status of his order like booked,pending completed in a fragment.I am right now doing it by calling api but everytime fragments gets created it calls api even if no data is changed.What is the possible and efficient solution of doing this.
For your scenario, the orders data are stored in your server-side database. Though you could store the retrieved order data into the local storage (e.g. SQLite,etc) on your mobile client, in order to retrieve the latest order status, you must explicitly call your mobile app backend for retrieving the latest order info and update your local order status. At this point, you could try to decrease the response data size returned by your backend via specifying minimum fields in your request. Details you could follow the .select() field selection clause.
Moreover, you could also follow Khemraj's suggestion about the notification approach, but the push notification may needs user interaction.

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?

how to edit database manually and update values in the android app?

We are developing an application called marketwatch.where we have to show prices of basic commodities,what we have to do is create a db of commodities and their prices and display them in our app whenever we change the value in db it should be updated in our android app.
If i understand you correctly, you want your applications database to be synchronized with your actual database.
You can do this by two ways:
Pulling: which means that every time you open the application or every period of time, maybe couple of hours or something, you request from the server the updates that happened in your database, if any
Pushing: which means that when your server has an update, it should notify the mobile app by sending a push notification to tell the client that there is an update to be fetched.
You can combine both, for example low priority updates should be retrieved by pulling, but high priority ones should be pushed to the client.
Have a look at the official training resources for Sync Adapters.
Or, if you don't have any constraints for the server part have a look at Firebase (from Google)

Android showing results of the query to the server

I develop app which will download information from Internet server and show search results in ListView. When user clicks on item more detailed information will be shown.
The question is whether to write search results before showing to DB or not? I know that Activity lifecycle can loose all data and some developers recomend to do it safe, but is it really necessary?
Yes, it makes sense to persist the data because if the activity is destroyed and you lose the data while the user has switched to instant messenger or mailer, the user will have to re-download the data from the server, which causes extra traffic and potentially money loss.
Also it doesn't necessarily need to be a DB - you can just use some temporary file. But you do need to persist this data.

Categories

Resources