Firebase Realtime Database do broadcast operation even when there is no subscriber - android

Recently I have came across with problem that Firebase Realtime Database generates too much download traffic.
In my application I only write to the database using:
database.child("child").setValue(value)
While researching the problem with Firebase's database profiler I found that there is listener_broadcast operation on each write operation and that broadcast generate download traffic despite the fact that I do not subscribe to read data anywhere.
Does anybody know how to fix this problem. Namely I would like to disable this broadcast operation

Related

sync local db (Sqlite) with server db android

I want to create a POS application but for some reason the application should be working when there is no internet connection (and later do the synchronisation when connect to internet). the only method that came to my mind is synchronising between local db with server but I dont have idea how to or the best method to do this. I have several question about this:
is it possible to use FCM cloud messaging to do this ? I mean Client A tell FCM data has changed, and then FCM tell several clients (phone) to do sync db in the background without user's (phone owner) confirmation (automatically).
or the only way is to use this method https://github.com/codepath/android_guides/wiki/Server-Synchronization-(SyncAdapter) ? if it's so, how often we can synch db local with server? is synchronising each 3 minutes will be fine?
Yes, use FCM real-time database for this. FCM manages data for you internally on the app, as well as on the cloud. So if you write some data into your JSON tree but you don't have internet connection, the data will be pushed into the cloud once the connection is available.
The documents clearly state this (Read last section on 'Writing Offline Data': https://firebase.google.com/docs/database/android/read-and-write
Also, any data added to the database when your client was offline will be sent once the client goes online - because the real-time database keeps storage of the data as well.
Using a server-sync every 3 minutes is not feasible - and it is more likely that your app will get killed by the system at some point or the other. Use FCM and Cloud Functions along with JobSchedulers to get the best result.

What Triggers the Firebase DISCONNECTED and NETWORK_ERROR DatabaseErrors?

I'm writing an Android application that uses the Firebase RealTime Database and Firebase Cloud functions. Several of my cloud functions are triggered by writes to the RealTime database and are used further process data saved by the Android client application.
I'd like my app to handle network connectivity changes gracefully. I understand that Firebase handles loss of connection by saving changes to the RealTime database locally and then syncing the changes to the server when connectivity is restored. This is well documented in the Firebase Documentation.
In my case though, since I need the Cloud Function post-processing of data saved to some paths to have occurred before it is useful, there's no point in having this data saved if it isn't going to make it to the server (and therefore trigger a Cloud Function), in a timely manner.
I'm using the updateChildren function from DatabaseReference to save the data and have a CompletionListener attached to monitor the outcome. I thought I may be able to use the DISCONNECTED and/or NETWORK_ERROR DatabaseErrors to identify cases where my data won't be reaching the server. However, if I interrupt the network connection before updateChildren is called, there aren't any errors generated. In this case, Firebase has likely saved the data locally with plans to sync it later, so updateChildren is considered to have been completed successfully.
My questions then are:
When are the DatabaseErrors DISCONNECTED and NETWORK_ERROR actually used by Firebase? Can I use them in some way to help manage connectivity issues?
What are the best practices for handling cases where Firebase data must make it to the server in order to be useful? Should I really just be POSTing my data to the Cloud Function directly?
Does Firebase have any notion of a timeout period that can be watched and data invalidated if it isn't synced within a specific period of time?
Yes, I recognize that I can use a listener attached to /.info/connected to detect changes in the connection state, but I'd rather be able to react and gracefully handle my case as it happens. I feel that my usage of the Realtime database together with Cloud Functions is common enough that there must be generally accepted way to implement it.
Any thoughts appreciated. Many thanks.
Did you try OnDisconnect method of firebase realtime database ?

Firebase' true offline capabilities

I'm trying to find out how far the offline capabilities of Firebase on Android actually go.
As far as I understand, it should be possible to make the Database "persistent" with FirebaseDatabase.getInstance().setPersistenceEnabled(true);
The documentation reads:
The Firebase Realtime Database stores data returned from a query for use when offline. For queries constructed while offline, the Firebase Realtime Database continues to work for previously loaded data. If the requested data hasn't loaded, the Firebase Realtime Database loads data from the local cache. When we come back online our data will load and reflect the query.
Is this also true when the offline state is beeing forced by goOffline?
In this question the user got an answer from firebase support:
While you can use goOffline() to force the client offline for a long time, performance will deteriorate over time. The Firebase clients will queue intermediate state changes, instead of updating the stored state as the server does.
Does this mean the "local database" isn't actually updated like it would be when offline due to connection loss?
Because most of the time any query or value event listener doesn't come back, onDataChange is never called as is onCanceled (I checked!)
If only the connection is lost, it actually works as advertised, although sometimes with up to a minute delay, which seems to be a problem on its own.
What is then the intention of even offering the goOffline() method if this just stops the interaction with the database completely?
In my implementation the app starts offline, with an anonymous authentication. So in the beginning of course the "local database" will be completely empty. But shouldn't the value events at least fire onDataChanged with an empty datasnapshot?
I tried staying online until I received the anonymous UID and added an empty entry into firebase database, which then is queried/cached. After that if I call goOffline, no more entries can be added and no more queries will be answered.
Similar to the above mentioned question, my plan is to offer the user the option to stay offline, with of course the downside of the build up of stored write events in the local cache (but that shouldn't be that big of a problem as there isn't that much data)
So how can I make this work if even possible at all?
The only thing I can see is to have some different database solution in the beginning for actual offline capabilities which has to be translated & transferred to firebase when the user chooses to go online.

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

Android bi-directional sqlite database synchronization

I've done a lot of search about this but I don't found anything really useful.
I just want to know if it is possible to sync a remote database with an android sqlite database using SyncAdapter and content providers.
I don't want to use another solution like webservices or sending the database file to server, and if possible I need to implement rules to resolve conflicts.
Thanks.
You'll still have to use something like web services to wire them together, there is no automatic mechanism to do that like sql replication or anything. All the SyncAdapter provides is a system managed periodically run service that's intended to be used for syncing data. You have to write all the code to talk to the local database and the remote database and manage moving the data between them.

Categories

Resources