Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline. Android

I get this run time error that I do not understand the reason behind it.

com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline.

Below is the code in my Activity that tries to fetch data from the cloud Firestore

DocumentReference docRef = db.collection("room-id-1").document("participan-name-1");
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document != null) {
                    Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
                    userData.registerUserToHotspot(roomId_str, participantName_str);
                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });

Is there something I can do about this?

like image 771
idriss saidalaoui Avatar asked Oct 07 '17 13:10

idriss saidalaoui


People also ask

Why am I getting an offline error when calling FireStore?

Sorry, something went wrong. That error might occur whenever device is offline and there is no data inside firestore local cache. So, try to wrap get calls in a try/catch, handle the failure normally and you should be fine. Sorry, something went wrong. Sign up for free to join this conversation on GitHub .

What is a firebasefirestoreexception?

When it comes in particular to Cloud Firestore, when something fails, there is a specific exception that is thrown which is called FirebaseFirestoreException. So according to the official documentation, this is: A class of exceptions thrown by Cloud Firestore.

Why is Cloud Firestore not secure enough?

Because all access to Cloud Firestore or the Realtime Database that comes from a backend server will bypass the security rules entirely. Security rules only apply to web and mobile client access. Always secure your database wisely, once to never get such an error and improve UX, but also to throw unwanted visitors away.

Is it possible to get error when the client is offline?

@ulver2812 Yes, if there is no cached data you will get an error when the client is offline. @PandaPalumbo Please make sure to use Firestore 7.15.1 (your log lines still say 7.15.0).


4 Answers

This happens because OnCompleteListener is triggered when the task is completed, either it fails or succeeds. To avoid the error, you can use separate OnSuccessListener and OnFailureListener.

OnSuccessListener is called when the task is succeeded, but if the above error occurs, OnFailureListener will be triggered and you can handle the error in onFailure() method of the listener.

Happy Coding :)

like image 127
Naman Jain Avatar answered Oct 18 '22 19:10

Naman Jain


If nothing helped and you sure that all good to go, do next steps:

  1. Clean project (Build -> Clean Project). You can also "Invalidate and Caches" with "Clean project" for more safety.
  2. Delete google-services.json from your project and sync.
  3. Build project. Android studio will throw build exception about missing google-services.json, It's okey.
  4. Download again google-services.json from Firebase console, put it in project and sync.
  5. Build and run.

Hoping I'm helped to some one

like image 3
Mopto Avatar answered Oct 18 '22 19:10

Mopto


I had the same exception when I used wrong path to the document. When I fixed the path it started to work.

Anyway, the error message is misleading.

like image 1
Andrew Avatar answered Oct 18 '22 18:10

Andrew


In my case the solution was to create a new emulator in Android Studio and run the app there

like image 1
marcolav Avatar answered Oct 18 '22 19:10

marcolav