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

Viewed 12122

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?

8 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 :)

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

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.

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

if you are using rxjava then it wiil be simple to catch throwable this way:

RxFirestore.getDocument(FirebaseFirestore.getInstance()
    .collection("info")
    .document("app"))
    .subscribeOn(Schedulers.io())
    .subscribe(snapshot -> {

    }, throwable -> Timber.e(throwable))

Otherwise try somehow catch it in promise or/and try-catch block

For me, I faced this issue because i forgot to initialize my firestore database from the Firebase console. Hope this helps someone else.

I faced this problem recently I tried deleting and re-installing the emulator still no dice. So I decided to catch the error and retry the query if the particular error is present. If the user was really offline I included a time out, if not it will retrieve the data after retries.

    fun makeFirestoreQuery(){
         fireStoreDb
        .collection(Constants.USER_DATA)
        .document(currentUserEmail)
        .get()
        .addOnSuccessListener{documentSnapshot->
        //perform opertion with the data
        }.addOnFailureListener{exception->
        if(exception.message?.contains("Failed to get document because 
        the client is offline")){
        makeFirestoreQuery() //recursively call itself
        //if the user is really offline, set a time out
        }}
    }

I faced with this problem on emulators below API 19 and asked Firebase support team. They responded to me that is a secure connection problem and they suggested to me enabling TLS on that emulators. As far as I understand, Firebase requires up-to-date security provider for connections. If you think all of your Firebase configurations are correct and still facing with this problem on emulators below API 19, be sure emulator has up-to-date security provider.

Please check "TLS on Android" section at this link: https://github.com/grpc/grpc-java/blob/master/SECURITY.md#tls-on-android

Google says it provides a up-to-date provider via Google Play Services. If you want to use that, there is a document.

https://developer.android.com/training/articles/security-gms-provider.html

Related