How to read data from Cloud Firestore using get()?

Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud.

Alex Mamo
Firebase Tips & Tricks

--

When it comes to reading data from Cloud Firestore, 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 addSnapshotListener() method. This method adds a listener for changes in the data at a collection in the database or a query. 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.

So Cloud Firestore, the newer massively scalable NoSQL cloud-hosted real-time database from Google, has a specific type of mechanism for reading the data once. To read a single document, we can use DocumentReference’s get() method that returns a Task<DocumentSnapshot>, while reading multiple documents from a collection or Query, we can use Firestore Query’s get() method that returns an object of type Task<QuerySnapshot>. Both methods read the data only once.

--

--