Is there a best practice on how to serve up singleton-like session data? I'd prefer to retrieve the user
record once in the beginning and then always get the same record back from memory whenever I am referencing it, similar to how Firebase Auth offers its data:
final FirebaseAuth auth = FirebaseAuth.instance;
After authentication, auth.currentUser!.uid
can be referenced anywhere and is clearly accessing the local cache of data relating to the authenticated user.
There is somewhat ambiguous language all over the Firestore docs about local caching, but I'm wondering if/when requesting a document is hitting an in-memory cache vs. going across the Internet to Firebase in order to serve a record that has already been retrieved prior.
For example, take a mock user document...
user
- email
And after successfully exiting an authentication flow...
FirebaseFirestore.instance.collection('user').user(authUserId).get()
to retrieve a record from Firestore collection user
for a document identified by the now-logged-in user, authUserId
.FirebaseFirestore.instance.collection('user').user(authUserId).get()
to fetch the same record in order to access user.email
.Is this two queries over the internet? Or 1 to the internet and then 1 local?
If the device is connected, both get()
calls will end up going to the server as the document may have changed between the calls.
If you want to prevent the second call going to the server, you can pass GetOptions
to the call.