I have setup my android firestore application similar to the example quickstart application, in other words:
EntryActivity
displays documents in a collection- When a document is clicked, pass the document ID to the
DetailsActivity
DetailsActivity
uses it's ownFirebaseFirestore
reference to read that document with the ID- Both the
EntryActivity
andDetailsActivity
obtain their ownFirebaseFirestore
reference throughFirebaseFirestore.getInstance()
inonCreate
However since the document being opened was previously read in the EntryActivity
, I want the DetailsActivity
to read it from the local cache faster than if it were read from the server.
In the DetailsActivity
, when requesting the document from cache:
var time = System.currentTimeMillis()
firestore.collection(collectionPath)
.document(docId)
.get(Source.CACHE)
.addOnSuccessListener { snapshot ->
time = System.currentTimeMillis() - time
Log.v(TAG, "read took $time ms fromCache=${snapshot.metadata.isFromCache}")
}
When used in DetailsActivity
:
read took 300-500 ms fromCache=false
So it is reading from the server, not the cache.
But to experiment I tried reading the document (after being already read) in the EntryActivity
using the same get(Source.CACHE)
method above, and it DOES read from cache like intended:
read took 30-80 ms fromCache=true
So it is apparent that it can only read from the cache when it has been read previously by the same FirebaseFirestore
instance, or in the same activity. Is it possible to read a document from cache when previously read by another activity?
Edit: As another experiment I tried sharing a FirebaseFirestore
reference in the Application
class across activities. In this case the second activity does read from cache, but the elapsed time is just as long as reading from the server:
read took 300-500 ms fromCache=true
Why is it still taking up to 500 ms to read from cache?
from Firestore app read from cache not working
No comments:
Post a Comment