My Firestore is still retrieving documents from cache even though I've explicitly told it not to:
class MainActivity : AppCompatActivity() {
val db = FirebaseFirestore.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dbSettings = FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build()
db.firestoreSettings = dbSettings
Above is my launcher activity where I set the Firestore settings.
In my fragment I perform a GeoFirestore GeoQuery
:
class MapFragment : Fragment() {
val instances = FirebaseFirestore.getInstance().collection("instances")
val geoFirestore = GeoFirestore(instances)
lateinit var nearbyDocs: GeoQuery
private fun searchNearby(){
nearbyDocs = geoFirestore.queryAtLocation(currentLocation, 1.0)
nearbyDocs.addGeoQueryDataEventListener(object : GeoQueryDataEventListener {
override fun onDocumentEntered(documentSnapshot: DocumentSnapshot, location: GeoPoint) {
Log.d(TAG, "onDocumentEntered: user1: ${documentSnapshot.getString("user1")?.substring(0, 3)} | docId: ${documentSnapshot.id.substring(0, 5)} | fromCache: ${documentSnapshot.metadata.isFromCache}")
Log.d(TAG, "isPersistanceEnabled2: ${db.firestoreSettings.isPersistenceEnabled}")
}
}
This is what it logs:
onDocumentEntered: user1: 0X6 | docId: lPLFf | fromCache: true
isPersistanceEnabled2: false
This doesn't make sense - I'm receiving cached documents even though my persistence is turned off.
Any idea what the problem is?
Edit: I have tried clearing the cache but that did not fix the problem.
This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data.
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors.
Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.
Use of the Firebase console will incur reads. If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads.
You are now able to choose if you would like to fetch your data from the server only, or from the cache only, like this (an example for server only):
DocumentReference documentReference= FirebaseFirestore.getInstance().document("example");
documentReference.get(Source.SERVER).addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
//...
}
});
For cache only, just change the code above to Source.CACHE.
By default, both methods still attempt server and fall back to the cache.
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