How to read data from Firebase Realtime Database using get()?

Firebase Realtime Database is a NoSQL cloud-hosted real-time database from Google.

Alex Mamo
Firebase Tips & Tricks

--

When it comes to reading data from Firebase Realtime Database, there are two options available. The first one is to read the data using a persistent listener, meaning that we’ll always be in sync with the Firebase servers, or we can read the data only once. The first option is very helpful when we need to listen for changes in real-time. We can achieve this using Query’s addValueEventListener() method. This method adds a listener for changes in the data at a particular path in the database. Each time the data changes, the listener will be invoked with an immutable snapshot of the data. However, there are cases in which we need to read the data from the database only once.

Even from the beginning in 2015, it was added in the Firebase Realtime Database Android SDK a method called addListenerForSingleValueEvent(). This method adds a listener for a single change, meaning that we can read the data at a particular path exactly once. This method remains part of the Firebase Realtime Database Android SDK ever since.

On the other hand, Cloud Firestore, the newer massively scalable NoSQL cloud-hosted real-time database from Google, has another type of mechanism…

--

--