Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore.getInstance(): how to use?

Firebase Firestore provides the getInstance() method, which opens a socket (only one, at any time of execution of the app) and instanciates the Firestore client.

I would want to query and store data, using or not listeners (to get realtime updates, etc.). If I call getInstance as soon as I need to query or store, or if I store this instance in a static class when my app starts and then use this static class attribute as soon as I need to query or store: these 2 cases are technically the same. Because Google uses singleton pattern (getInstance()).

But am I missing something? Is it actually safe to store this instance as a static class attribute, and use it when I need it? And is it really safe to call getInstance whenever I need it? To be more explicit: between 2 calls to getInstance() (or between 2 accesses to the static class attribute), i.e.: between 2 points of execution time, is there any risk to loose network connection, socket connection, realtime listeners (snapshots) connection, etc. ?

If yes: How to handle these problem(s)?

like image 746
JarsOfJam-Scheduler Avatar asked Feb 15 '19 21:02

JarsOfJam-Scheduler


People also ask

What does Firebase firestore () do?

Cloud Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps - at global scale.

How do I use firestore in Python?

First, we have to import the libraries and initialize the SDK using our downloaded service account credentials. Now we have to initialize the firestore instance. Now in the Firebase portal, go to the Database section and click Create Database to create a new database.


2 Answers

To complement Alex Mamo's answer, and avoid repeating FirebaseFirestore.getInstance() everywhere, I declared a static method called db which returns the result of the getInstance :

public static FirebaseFirestore db(){
    return FirebaseFirestore.getInstance();
};

Then I use it like that :

db().collection(...)
like image 44
DecibHell Avatar answered Oct 12 '22 06:10

DecibHell


I would want to query and store data, using or not listeners (to get realtime updates, etc.)

There is no way to get data or even get realtime updates without using a listener. Everything in Cloud Firestore is about listeners.

if I store this instance in a static class when my app starts and then use this static class attribute as soon as I need to query or store

Do not place Android context classes in static fields. Static reference to FirebaseFirestore which has field context pointing to Context will lead to a memory leak.

A static field will leak contexts. Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a reference to the activity which prevents it from getting garbage collected.

So instead of storing it as a static variable call getInstance() whenever is needed. Or a more convenient solution would to use dependency injection. Dagger can help you solve that.

And is it really safe to call getInstance whenever I need it?

Yes it is.

between 2 accesses to the static class attribute), i.e.: between 2 points of execution time, is there any risk to loose network connection, socket connection, realtime listeners (snapshots) connection, etc. ?

Please see explanation above.

like image 79
Alex Mamo Avatar answered Oct 12 '22 06:10

Alex Mamo