Skip to content Skip to sidebar Skip to footer

How To Include Source Cache In Cloud Firestore Realtime Update In Mvvm Architecture Android

In my app i am using android MVVM architecture, so for retrieving data from cloud firestore i am using layers so i create one more class (FirebaseQueryLiveData) for getting result

Solution 1:

For Android and iOS, Cloud Firestore has offline persistence enabled by default. This means that your app will work for short to intermediate periods of being disconnected.

And yes, you can specify the source with the help of the DocumentReference.get(Source source) and Query.get(Source source) methods.

By default, get() attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. This behavior can be altered via the Source parameter.

So we can now pass as an argument to the DocumentReference or to the Query the source so we can force the retrieval of data from the chache only like this:

FirebaseFirestoredb= FirebaseFirestore.getInstance();
DocumentReferencedocIdRef= db.collection("tests").document("fOpCiqmUjAzjnZimjd5c");
docIdRef.get(Source.CACHE).addOnSuccessListener(newOnSuccessListener<DocumentSnapshot>() {
    @OverridepublicvoidonSuccess(DocumentSnapshot documentSnapshot) {
        //Get data from the documentSnapshot object
    }
});

In this case, we force the data to be retrieved from the cache only but why to use this feature when you say that you want to get realtime updates? So for your use-case I don't see why you would get the data from cache.

Post a Comment for "How To Include Source Cache In Cloud Firestore Realtime Update In Mvvm Architecture Android"