由于客户端处于离线状态

由于客户端处于离线状态

本文介绍了com.google.firebase.firestore.FirebaseFirestoreException:由于客户端处于离线状态,因此无法获取文档.安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此运行时错误,我不理解其背后的原因.

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.

下面是我的活动中的代码,该代码试图从云Firestore中获取数据

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());
            }
        }
    });

对此我能做些什么吗?

推荐答案

之所以会发生这种情况,是因为OnCompleteListener在任务完成时被触发,无论是失败还是成功.为避免该错误,可以使用单独的OnSuccessListenerOnFailureListener.

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,但是如果发生上述错误,则会触发OnFailureListener,您可以在侦听器的onFailure()方法中处理该错误.

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.

快乐编码:)

这篇关于com.google.firebase.firestore.FirebaseFirestoreException:由于客户端处于离线状态,因此无法获取文档.安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:07