Sandeep Choudhary
Sandeep Choudhary

Reputation: 260

Firestore: Fetch data from server only if there is an update else fetch from cache

I started working on the Firebase Firestore database to utilize my lockdown time. I created an app where I have some master data stores in the Firestore and some transaction data. Now for master data, I currently fetch like this

firestoreInstance.collection("makes-models")
            .whereEqualTo("type", "2W")
            .get()
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    makesListMutableLiveData.setValue(null);
                    Log.e(TAG, e.getMessage());
                }
            }).addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful()) {
                        List<Make> makes = task.getResult().toObjects(Make.class);
                        makesListMutableLiveData.setValue(makes);
                    } else {
                        makesListMutableLiveData.setValue(null);
                        Log.e(TAG, task.getException().getMessage());
                    }
                }
            });

Now what I want is, I should only fetch the data from the server if there is any kind of modification or else the data must be fetched from CACHE only. As per the documentation, I read, there are ways to fetch

  1. only from cache
  2. only from server
  3. check server and if not able to connect due to network issue, fetch from local.

What I want is:

Somehow to know if there is an update in data at backend, if yes, fetch from server or else, fetch from local. One thing which came to my mind is to use check a timestamp from the server since the last fetch and work accordingly, but this seems like too much overhead and I am assuming there must be a better way.

Upvotes: 1

Views: 1983

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138969

When you are making a get() call, the Firestore SDK will always try to get the data from the server, no matter if the data is changed or not. You'll be able to get the data from the cache, only if the device goes offline. However, if you trying to listen for changes in real-time, according to the official documentation:

The initial state can come from the server directly, or from a local cache. If there is a state available in a local cache, the query snapshot will be initially populated with the cached data, then updated with the server's data when the client has caught up with the server's state.

In other words, if the server says that you have no new added/updated/deleted documents, you get the data from the cache. However, there is something else you should take care regarding the duration of time you can get the data from cache:

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

Another possible charge might also come from:

There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

But all these charges are normal according to how is Firestore designed.

I have also written an article that you might be interested in:

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317712

There is no conditional get() in Firestore. The three cases you outlines are the only cases supported. If you want to know if the query results would be changed from the server, you will actually need to query the server, and pay the full cost of that query.

Some viable alternatives are:

  • Have the server notify the client of any data that might have changed using some sort of push notification. The server will have to know which documents are interesting to the client, and the client will need to fetch only those documents as needed.

  • Create another document that holds the last update time of the documents in the collection of interest. The client can check that one document to see if something might have changed, then make a decision about how to query them.

The bottom line is that you're going to have to invent a way to indicate to the client if it should query cache or server at any given moment.

Upvotes: 1

Related Questions