Firestore still retrieving documents from cache even though I've disabled persistence
Asked Answered
E

1

10

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.

Egor answered 20/7, 2019 at 8:58 Comment(4)
Please edit the question to explain exactly how you are observing that documents are definitely coming from the cache and not the server.Faircloth
Edited - when it happens again I will include the cached document log.Egor
Probably, Doug didn't see your response since you haven't tagged him with @. He could have helped, I guessStruma
@DougStevenson I've updated my answer with some examples of cache retrievalsEgor
K
4

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.

Kitten answered 2/8, 2019 at 10:18 Comment(2)
Thanks this would solve my problem - but I'm using a library for my queries: github.com/imperiumlabs/GeoFirestore-Android so I don't have direct access to my code.Egor
@Zorgan, since geofirestore doesn't have an option to force server reads, you can simply add this library as a module and change this specific partStruma

© 2022 - 2024 — McMap. All rights reserved.