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)?
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.
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.
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(...)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With