I am using firebase offline capabilities to fetch data from cache once it load all data in local disk even if my internet is on.I successfully managed to do it but when ever I read data in my app, my firebase data download size increases. I am confused: when I get data from local cache, my data download size increases every time I am reading data in my app. If anyone knows please help me out.
Even when the data is already in the cache, the Firebase client will connect to the server and check if the cached data is up to date. This check is typically significantly smaller than downloading the actual data, but you'll still be charged for the bandwidth it consumes.
If you want to work completely offline, and don't want the client to check with the server at all, you can tell the Firebase client to goOffline when the app starts.
Related
It is about Firebase's local cached data in Android.
If the Android user, has 1-2 years data in Firebase,
is there a way to enable, setup, or filter Firebase persistence in local,
so that it only create local cache for the last month ?
So we don't the whole data in local cache, but only the last month.
The Firebase local cache is not designed to be manually populated. It's managed automatically, and you don't have much control over what gets stored in it. It will store data from recent queries, and it will evict data that hasn't been used. You can't choose which data will or will not be cached - it work with everything or nothing at all.
The local cache is meant to be helpful when the user temporarily loses their network connection. It's not meant for full offline support.
If you need a cache for very specific data that you can control, you will need to build that yourself.
The use case is a dual platform mobile app for an event. There is a schedule with photos, links, bios of the speakers and talk descriptions. If all of the attendees happen to download and open the app at the same time and in the same place, they might not get the best experience -> the WiFi might slow the calls into the data server, calls into the FireBase server side will spike.
Is it possible to export a database from the server side and preload the event schedule data into the mobile app download? Upon launch the app can sync any last minute updates, as needed, with a connection and a short sync to Firebase.
If this type of architecture is not available, is there an alternative that the Firebase team would recommend?
There is no way within the Firebase Database API to preload data into the disk cache.
Two things I can think of (neither of them very nice):
Have the client read the JSON file from your app's resources and write it to the location. The end result of this will be that the data on the server stays unmodified. But it does result in each client writing the same data to the server, so the inverse of your original problem (and likely worse performing).
Have a wrapper around the Firebase API calls that loads from the JSON file and then have them later attach listeners after a random delay (to reduce the rush on the app).
As said, neither of them is very good. For both of them, you can download the JSON from the Firebase Database console.
In my experience the usage of conference apps is a lot lower than most developers/organizers imagine. It's also typically quite well spread out over the duration of the conference. So reducing the amount of data you load might be enough to make things work.
On android you can ship a sql database in the assets directory with the app and then reconcile it with the updates when the users open the app. The Firebase database is a json file. You could also ship that in the assets directory and then reconcile on first load.
I have an application that pulls some simple data from Firebase Realtime Database, and to make things simple here is the flow that I want to have.
If phone is connected to the internet:
Get the data from the firebase database
If phone is offline
If there is data in firebase cache -> get data from cache
If there is nothing in firebase cache -> show some default data stored locally in app
The problem is that I don't see possible way to detect if data is available in firebase cache. So when phone is offline I cannot see if I should display the locally stored data or cache from firebase database.
Agree above answer, Firebase will do it for you, if you do it in the right way. In order to solve your problem, you should understand the difference between the two approaches of firebase offline cache - reading-from-memory and reading-from-disk.
Read this article -> https://pamartinezandres.com/lessons-learnt-the-hard-way-using-firebase-realtime-database-c609b52b9afb
in your case, just add, FirebaseDatabase.getInstance().setPersistenceEnabled(true); to enable reading from disk
and add yourReferenceForFirebase.keepSynced(true); to keep the data synched
then your app will work as you expected.
But, only this part: "If there is nothing in firebase cache -> show some default data stored locally in app", seems impossible.
Firebase does it for you.
Firebase applications work even if your app temporarily loses its network connection.
Also firebase apps automatically handle temporary network interruptions.
Cached data is available while offline and Firebase resends any writes when network connectivity is restored.
If it is not enough and you need to work offline you can also enable the disk persistence just adding:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
More info here.
EDIT:
There is no way to know whether a value comes from the local cache or from the remote server.
There is only a way to know if your app is currently connected to the Firebase Database server.
To achieve it you can detect the connection state by listening for the .info/connected value.
I am using kinvey as a backend to store some data. Everytime the user answers some questions on the phone(Android), the data is sent to kinvey immediately. But there can be a scenario where the user is not connected to the internet, so sending the data has failed.
I would like to know if there is any standard way to storing the data that hasnt been send in an efficient manner? Also how often should I try to resend the data that has not been sent? I would typically just put it in a db, and try to send whatever is in that table every 30 minutes or so and clear the table if the send is successful?
Would this be ideal? Also I see that Kinvey doesnt have a mechanism to handle this automatically(correct me if I'm wrong).
Thank you for any suggestions.
Local database is a good place to store your data securely. The thing is that you are sending data after every 30 min, you have to change that scenario.
Below are the steps:
Check Internet connectivity, if available than work online no need to store data locally
If internet is not available than store it in local DB
You are able to get Internet connection broadcast from system whether it is connected or not if you get connection at that time sync your data with the server (No need to check every 30 min) (System always broadcast when it get connection)
You can took a look at Firebase, feature that you're looking for is described in Offline Capabilities. If migrating from Kinvey is not an option - then store your data inside a DB and use JobScheduler to sync it to the backend when the conditions are right.
The other answers cover most of the use cases.
But if you don't want to use system broadcasts, another alternative would be for you to store the data on a database (SQLite, Firebase, etc), and try some sort of exponential backoff strategy to send your data to the server, using the AlarmManager for example. (Tutorial here).
You can try to check the internet connection from time to time, and send the data. If it fails for some reason (slow connection, connection drops, or no connection at all), try again later. Once you succeed, you can either remove the data from your data storage, or set up a flag like isSent = true.
Kinvey is rolling out offline sync capabilities to all their SDK's, it's currently in Beta. If you use iOS or Javascript for mobile development, check out the "3.0 beta" SDK's. You mentioned you use Android, support for that platform will come soon as well.
In your case, you need a cache store, it will save the data locally and sync automatically if there is no network. You don't have to worry about when or how often to sync.
http://devcenter.kinvey.com/ios-v3.0/guides/datastore#CacheStore
I'm developing a real time data collection application of Android.
Currently I produce ~1.2 KB of data each second, and i need to upload them each second to a remote server through a REST API.
Problem is that this data is critical and I can't tolerate any loss. So I need to cache them on disk and upload the data to remote server. After a confirmed delivery of each data, it can be deleted from the cache, otherwise cache must be preserved until it gets uploaded successfully, even after a device reboot.
What are the the best approached to solve this problem in your point of views?
What are the tools/frameworks/libraries that might be helpful in creating the solution?
I would go with Square Tape (http://square.github.io/tape/) it guarantees that data will be written to disk no matter what. You can handle then your own logic of syncing it and re-queueing in case of a failure.