'Accessing same Firebase Firestore in multiple fragments
I have an ArrayList<Person> stored in Firestore.
I have a RecyclerView in Fragment A displaying the basic info of each person.
I have an onClickListener that replaces Fragment A with Fragment B and passes off the UID of the Person on Item clicked to Fragment B as well.
My goal with Fragment B is to display all of the details of the Person item clicked (and an option to add some more data) by somehow using the UID to get my data from Firestore.
My question is this, should I instantiate a Firebase Firestore for every Fragment that I need that same Firestore in? Or can I pass off the Person object as a bundle (though I'm not sure if any changes I make reflect on Firebase).
I thought it was a very costly process and I'm not sure what my alternatives are.
Solution 1:[1]
- FirebaseFirestore is a Singleton class which means that any where you call this:
FirebaseFirestore db = FirebaseFirestore.getInstance();
You will always get the same object.
You can pass off the data to the second fragment if the Person class implements Parcelable. If it implements Parcelable you can add the person object to a bundle and send it to the other fragment this way.
If by "instantiating Firestore" you actually mean retrieving your data from Firestore , Firestore locally caches a copy of the data that your app is using, so fragment B will retrieve the data from the local cache.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
