Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Disable Offline Data In Firestore

Tags:

After deleting data from my Firestore Database, it takes my Android app some time to realize that the data was deleted, and I assume that it's happening due the auto data cache. My app has nothing to do with offline usage and I'd like to disable this feature...

I have added this in my custom Application Class:

import android.app.Application; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.FirebaseFirestoreSettings;  public class ApplicationClass extends Application {      @Override     public void onCreate() {         super.onCreate();          FirebaseFirestore db=FirebaseFirestore.getInstance();         FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()                 .setPersistenceEnabled(false)                 .build();         db.setFirestoreSettings(settings);     } } 

The problem occurs after turning off the internet connection and than turning it back on (while the app is still running, in the background or not)- the Firestore module seems to lose connection to the server, and it makes the opposite operation than the intended one - instead of stop taking data from the cache, it takes data from the cache only.

For example, debugging this code will always show that isFromCache is true and documentSnapshot is empty (even though that on the server side - it's not empty):

usersRef.document(loggedEmail).collection("challenges_received").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {     @Override     public void onSuccess(QuerySnapshot documentSnapshots) {         boolean isFromCache=documentSnapshots.getMetadata().isFromCache();         if (!documentSnapshots.isEmpty()) {         }     } }); 

Is this normal behavior?

Is there another way to disable the data cache in Cloud Firestore?


EDIT:

Adding: FirebaseFirestore.setLoggingEnabled(flase); (instead of the code above) in the custom Application Class gives the same result.

like image 473
Tal Barda Avatar asked Oct 23 '17 16:10

Tal Barda


People also ask

What is offline data persistence?

When you enable disk persistence, your app writes the data locally to the device so your app can maintain state while offline, even if the user or operating system restarts the app. You can enable disk persistence with just one line of code.

Can firestore work offline?

Note: Offline persistence is supported only in Android, Apple, and web apps. To use offline persistence, you don't need to make any changes to the code that you use to access Firestore data.

How do I use Firebase persistence?

For Android and Apple platforms, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false . For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method.


1 Answers

According to Cloud Firestore 16.0.0 SDK update, there is now a solution to this problem:

enter image description here


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 72
Tal Barda Avatar answered Oct 02 '22 16:10

Tal Barda