Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 950
Zorgan Avatar asked Jul 20 '19 08:07

Zorgan


People also ask

Does firebase cache read?

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.

How do I delete all Subcollection on firestore?

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.

Does firestore Create collection if not exists?

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.

Why are my firestore reads so high?

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.


1 Answers

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.

like image 86
Pranav Darji Avatar answered Oct 16 '22 20:10

Pranav Darji