Can't Disable Offline Data In Firestore

can definition
can synonym
can verb
can y
can noun meaning
can band
cant
can't

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.

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


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.

May I? Can I? - English Grammar - English, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

I just ran a few tests in an Android application to see how this works. Because Firestore is currently still in beta release and the product might suffer changes any time, i cannot guarantee that this behaviour will still hold in the future.

db.collection("tests").document("fOpCiqmUjAzjnZimjd5c").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        DocumentSnapshot documentSnapshot = task.getResult();
        System.out.println("isFromCache: " + documentSnapshot.getMetadata().isFromCache());
    }
});

Regarding the code, is the same no matter if we're getting the data from the cache or you are connected to the servers.

When I'm online it prints:

isFromCache: false

When I'm offline, it prints:

isFromCache: true

So, for the moment, there is no way to stop the retrieval of the data from the cache while you are not connected to the server, as you cannot force the retrieval of the data from the cache while you're connected to the server.

If instead I use a listener:

db.collection("tests").document("fOpCiqmUjAzjnZimjd5c").addSnapshotListener(new DocumentListenOptions().includeMetadataChanges(), new EventListener<DocumentSnapshot>() {
    @Override
    public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
        System.out.println("listener.isFromCache: " + documentSnapshot.getMetadata().isFromCache());
    }
});

I get two prints when I'm online:

listener.isFromCache: true
listener.isFromCache: false

Firestore is desinged to retrieve data from the chache when the device is permanently offline or while your application temporarily loses its network connection and for the moment you cannot change this behaviour.

As a concusion, an API that does something like this, currently doesn't exist yet.

Edit: Unlike in Firebase, where to enable the offline persistence you need use this line of code:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

In Firestore, for Android and iOS, offline persistence is enabled by default.

Using the above line of code, means that you tell Firebase to create a local (internal) copy of your database so that your app can work even if it temporarily loses its network connection.

In Firestore we find the opposite, to disable persistence, we need to set the PersistenceEnabled option to false. This means that you tell Firestore not to create a local copy of your database on user device, which in term means that you'll not be able to query your database unless your are connected to Firebase servers. So without having a local copy of your database and if beeing disconected, an Exception will be thrown. That's why is a good practice to use the OnFailureListener.

Update (2018-06-13): As also @TalBarda mentioned in his answer this is now possible starting with the 16.0.0 SDK version update. So we can achieve this 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 server only, chache only or attempt server and fall back to the cache.

So something like this is now possible:

FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference docIdRef = db.collection("tests").document("fOpCiqmUjAzjnZimjd5c");
docIdRef.get(Source.SERVER).addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        //Get data from the documentSnapshot object
    }
});

In this case, we force the data to be retrieved from the server only. If you want to force the data to be retrieved from the cache only, you should pass as an argument to the get() method, Source.CACHE. More informations here.

Use can in a sentence, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder() .setPersistenceEnabled(false) .build(); dbEventHome.setFirestoreSettings(settings);

By setting this it is fetching from server always.

CAN (noun) definition and synonyms, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

// Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(flase);
// Firestore
    mFirestore = FirebaseFirestore.getInstance();

In general: the Firebase client tries to minimize the number of times it downloads data. But it also tries to minimize the amount of memory/disk space it uses.

The exact behavior depends on many things, such as whether the another listener has remained active on that location and whether you're using disk persistence. If you have two listeners for the same (or overlapping) data, updates will only be downloaded once. But if you remove the last listener for a location, the data for that location is removed from the (memory and/or disk) cache.

Without seeing a complete piece of code, it's hard to tell what will happen in your case.

Alternatively: you can check for yourself by enabling Firebase's logging [Firebase setLoggingEnabled:YES];

try this For FireBase DataBase

  mDatabase.getReference().keepSynced(false);      
      FirebaseDatabase.getInstance().setPersistenceEnabled(false);

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. A Controller Area Network (CAN bus) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other's applications without a host computer. It is a message-based protocol , designed originally for multiplex electrical wiring within automobiles to save on copper, but can also be used in many other

Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Can(stylised as CAN) was a German experimental rockband formed in Colognein 1968 by the core quartet of Holger Czukay(bass, tape editing), Irmin Schmidt(keyboards), Michael Karoli(guitar), and Jaki Liebezeit(drums).

The Official CAN / Spoon Records Website, Can-Am offers a complete lineup of Off-Road vehicles (ATV, Side-by-Side) and 3-​wheel motorcycles (Spyder or Ryker). Choose your playground now and enjoy  We would like to show you a description here but the site won’t allow us.

Can, Can-Am Off-Road is an excellent place for those who dare to venture off-road. Discover our 2020 lineup: Side by Side and ATV for performance, adventure or  Create beautiful designs with your team. Use Canva's drag-and-drop feature and layouts to design, share and print business cards, logos, presentations and more.

Comments
  • mDatabase.getReference().keepSynced(false); FirebaseDatabase.getInstance().setPersistenceEnabled(false);
  • look personally i don't think that it has anything to do with the cache still in case it has that problem then check enable offline persistence(firebase.google.com/docs/firestore/manage-data/…)
  • Please consider easing up on the minor edits here. The question already has a bounty set, so it'll be featured for 5 more days without you needing to "bump" it with edits.
  • Did you try this: stackoverflow.com/questions/46790741/…
  • @AlexanderVitanov It seems like an option to consider if I won't find the solution. However, if I have checked whether the snapshot is from cache or not and I found out it does, will I be able to get the snapshot from the server instead? Can I choose?
  • If Firestore is designed to retrieve data from the cache (after a disconnection) no matter what, then what "setPersistenceEnabled(false)" feature is good for?
  • I exaplained in my updated answer why is setPersistenceEnabled(false) feature is good for. Please take a look.
  • Thanks for the answer. If I put FirebaseFirestore.setLoggingEnabled(false) instead of setPersistenceEnabled(false) the result stays the same, I can't make any requests to the server after going offline and than back online... And regarding to the thing the you wrote under "try this", I think it's for Realtime Database, not Firestore.
  • Will review it again