com.google.firebase.firestore.FirebaseFirestoreException:由于客户端处于脱机状态,因此无法获取文档。 Android的

时间:2017-10-07 13:52:27

标签: android firebase google-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()); } } });

以下是我的Activity中尝试从云Firestore中获取数据的代码

Settings->Reading

我能为此做点什么吗?

7 个答案:

答案 0 :(得分:1)

就我而言,解决方案是在Android Studio中创建一个新的模拟器并在其中运行应用程序

答案 1 :(得分:0)

如果您使用的是rxjava,那么以这种方式抓住throwable很简单:

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

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

否则尝试以某种方式在promise或/和try-catch块中捕获它

答案 2 :(得分:0)

当我使用错误的文档路径时,我遇到了同样的异常。当我固定路径时,它开始起作用。

无论如何,错误消息是令人误解的。

答案 3 :(得分:0)

之所以会发生这种情况,是因为任务完成或完成后都会触发OnCompleteListener。为避免该错误,可以使用单独的OnSuccessListenerOnFailureListener

OnSuccessListener在任务成功完成时被调用,但是如果发生上述错误,将触发OnFailureListener,您可以使用侦听器的onFailure()方法处理该错误。

快乐编码:)

答案 4 :(得分:0)

对我来说,我遇到了这个问题,因为我忘记了从Firebase控制台初始化Firestore数据库。希望这对其他人有帮助。

答案 5 :(得分:0)

我最近遇到了这个问题,我尝试删除并重新安装模拟器仍然没有问题。因此,我决定捕获错误,如果存在特定错误,请重试查询。如果用户确实处于脱机状态,那么我会设置一个超时时间;否则,将在重试后检索数据。

    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
        }}
    }

答案 6 :(得分:0)

请检查设备中的互联网连接。 对于Internet连接问题,会发生这种类型的错误。...

相关问题