I'm developing an Android application that uses a lot data from and towards my Firestore database.
Here is an overview of what I do.
Uses from: I set up listeners on documents and collections, and I use the method
get
;Uses towards: I store documents, using and not using
set
, I also update documents.
For now, each time I do any of these mentionned actions, I instanciate a class named SetupFirebaseFirestore
. This is an AsyncTask
which instanciates Firestore. When it's finished, it also executes a callback (given when I instanciate SetupFirebaseFirestore
).
You can find the code below:
public class SetupFirebaseFirestore extends AsyncTask<Void, Void, Void> {
private SetupFirebaseFirestorePostExecuteCallback post_execute_callback;
private FirebaseFirestore db;
public SetupFirebaseFirestore(SetupFirebaseFirestorePostExecuteCallback callback) {
post_execute_callback = callback;
}
private void createFirebaseProducts() {
db = FirebaseFirestore.getInstance();
db.setFirestoreSettings(new FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build());
}
@Override
protected Void doInBackground(Void[] params) {
createFirebaseProducts();
return null;
}
@Override
protected void onPostExecute(Void result) {
post_execute_callback.onTaskCompleted(db);
}
}
The problem and The question
The problem is that my application interacts a lot with Firestore. So there are many instanciations of Firestore, and thus many RAM consumption.
By the way, perhaps it overloads Firestore since I call getInstance()
and build()
?
How could I do something more clean in terms of RAM consumption? Should I use singleton pattern? It would allow me to instanciate Firestore once, and the same for building its settings.