Firebase how to detect if cached data is available - android

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.

Related

Firestore: How to cache writes+changes and upload only at the end on mobile client

Please, I have a project in which a user needs to configure an object(e.g his shop). So I want the user to be able to read from Firestore during the modification but not to write to the online database directly. Instead, I wish he/she should write or modify to his offline cache and when he taps the "save" button, all changes should be committed online.
Thanks
I wish he/she should write or modify to his offline cache and when he taps the "save" button, all changes should be committed online.
There is no way you can tell Firestore to write the data to cache. This is happening by default when the user has no internet connection. According to the docs, Firestore has offline persistence enabled by default:
For Android and iOS, offline persistence is enabled by default.
However, you can tell Firestore to read the data only from the cache by specifying the source.
So when the user finishes configuring the shop, there are two cases. The first one is when the user has internet connectivity, meaning that all data is saved on the Firebase server, or when the user has no internet connectivity and the data is written to the cache. Once the user regains connectivity, all data is sent to the Cloud.
If you want to save multiple operations when configuring a shop, then you might also consider using SharedPreferences, which is available across your entire application.

Android Firebase - Local Cached Data

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.

Firebase offline capabilities causing memory problems

I am developing a chat app that uses firebase database to store data. The usual approach while developing a chat app is to keep the database nodes synced so that you access the messages offline. So the problem rises when I implement the firebase offline capabilities to keep the data nodes synced. Firebase suggests two required steps for accessing data offline:
Enabling disk persistance
this is enabled according to the documentation by using this line of code (in my case I add it in application class):
FirebaseDatabase.getInstance().setPersistanceEnabled(true);
and
Keeping a node synced
this is enabled by simply adding keepsynced(true) to any databasereference that you wish to keep synced, like this:
ChatNode.keepSynced(true);
What is the difference between the two?
According to the firebase team answers on this site, I deduced that:
1) (Disk persistance) stores the data on the device disk to use them when needed, and data is stored wether you write data or read data.
a) If you write data offline: data is stored on disk and is sent to database when you go online again.
b) If you read data offline: the listener that was read online and was kept in disk and stored, you will be able to read it offline from disk.
2) (keep synced true) will keep a database reference synced in 2 ways:
a) If you are also using (disk persistence) with (keep synced) you will be able to keep data synced on disk ... which seems to be the default behavior of (disk persistence).
b) If you are using (keep synced) alone then you only store to what is known as the app memory.
The problem
I did set both of the methods, but my app is now very laggy and slow and sometimes stops on its own.
The question
If all the things that I said above are true, then would this method of offline capability be a heavy load on my app?
If I kept many listeners synced and set persistence enabled, then would the disk become full of data? Should I clean the data? Is the data on disk cleaned by itself in both methods? Is data cleaned by itself from memory?
I want to avoid the lagging and slow response in my app, thanks for your help.
You are right about your assumptions. If you are using FirebaseDatabase.getInstance().setPersistenceEnabled(true); means that Firebase will create a local copy of your database which also means that every change that is made while you are offline, will be added to a queue. So, as this queue grows, local operations and application startup will slow down. So the speed depends on the dimension of that queue. But rememeber, Firebase is designed as an online database that can work for short to intermediate periods of being disconnected and not as an offline database.
Second, if are using many listeners, don't forget to remove the listener accordingly to the life-cycle of your activity like this:
databaseReference.removeEventListener(valueEventListener);

Firebase Realtime Database: optimizing for local usage

In my use case, the data is only written by client, not by server.
But I would like to use Cloud Functions on the server, whenever a change is made on the client.
What I would like to avoid is to re-download the data from the server if it's already locally stored.
I found out is it possible to enable disk persistance:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Can anyone explain if this actually prevents re-downloading data in case it's already locally stored, or the data would periodically re-downloaded anyway?
Generally speaking, which is the criteria by which the data is re-downloaded? Is there a sort of hashcode check between client and server data to find out if the data has changed?
Calling setPersistenceEnabled(true) enabled Firebase's disk cache. It's primary purpose is to ensure your app continues to work when there is no network connection. In addition it may reduce data transfer, but that depends on quite a few variables.
For some more information on what disk persistence means, how it interacts with Firebase's in-memory caching of data, and more, see:
Firebase : What is the difference between setPersistenceEnabled and keepSynced?
Firebase offline capabilities as cache
Firebase Offline Capabilities and addListenerForSingleValueEvent (just an interesting/annoying edge-case)

Can I use Firebase for applications that has just database operations?

I am working on a budget management application, which needs to store users incomes and expenses then show back to user. I am using SQLite in my project which takes lots of code to save and retrieve data. I wonder whether I can use Firebase just to save and retrieve data. Any answer is appreciated.
Yes, sure you can! Just just use the Firebase Realtime Database service. Think of it like a JSON-ish storage in the cloud :-)
As a bonus, you can enable offline persistence and query your data even offline:
Firebase applications work even if your app temporarily loses its network connection. In addition, Firebase provides tools for persisting data locally, managing presence, and handling latency.
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 network connectivity is available again, the data loads and will reflect the query.
You can enable disk persistence with just one line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Firebase database writes, while offline, would be added to a queue which would sync once network is back and online.

Categories

Resources