com.google.firebase.firestore.FirebaseFirestoreExc

2020-06-07 07:19发布

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?

4条回答
手持菜刀,她持情操
2楼-- · 2020-06-07 08:07

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

查看更多
Anthone
3楼-- · 2020-06-07 08:22

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

查看更多
你好瞎i
4楼-- · 2020-06-07 08:23

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.

查看更多
时光不老,我们不散
5楼-- · 2020-06-07 08:23

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

查看更多
登录 后发表回答