【问题标题】:com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline. Androidcom.google.firebase.firestore.FirebaseFirestoreException:获取文档失败,因为客户端离线。安卓
【发布时间】:2018-03-19 03:48:09
【问题描述】:

我收到此运行时错误,我不明白其背后的原因。

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

下面是我的 Activity 中尝试从云 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());
            }
        }
    });

对此我能做些什么吗?

【问题讨论】:

  • 我遇到了同样的问题。 FirebaseFirestoreException.Code 为“不可用”。这意味着“服务当前不可用。这很可能是暂时的情况,可以通过回退重试来纠正。”我不知道我现在该怎么办......
  • 同样的问题,有解决办法吗?
  • 我看到这个错误是因为你没有连接或客户端不可用,perharps,我遇到了问题,因为即使我在,以与您相同的方式管理异常。更有趣的是,在恢复我的互联网后,第一个查询总是会发生错误。示例:Flight mode &gt; Do nothing &gt; off Flight mode &gt; wait 20s &gt; query something &gt; Failed to get document because the client is offline. 这是来自 API 的错误?这只是在 Android 中发生,而不是在 IOS 中发生
  • 这方面的任何进展,这绝对是一个交易破坏者。无法在 Android 上使用 Firestore?
  • 嗨!有这方面的消息吗?我今天在 firebase-firestore:17.1.1 上仍然面临完全相同的问题。

标签: android firebase google-cloud-firestore


【解决方案1】:

这是因为OnCompleteListener 在任务完成时触发,要么失败,要么成功。为避免该错误,您可以使用单独的OnSuccessListenerOnFailureListener

任务成功时会调用OnSuccessListener,但如果出现上述错误,则会触发OnFailureListener,您可以在监听器的onFailure()方法中处理错误。

快乐编码:)

【讨论】:

    【解决方案2】:

    如果没有任何帮助,并且您确定一切顺利,请执行以下步骤:

    1. 清理项目(构建 -> 清理项目)。您还可以使用“清理项目”来“无效和缓存”以提高安全性。
    2. 从您的项目中删除 google-services.json 并同步。
    3. 构建项目。 Android Studio 将抛出关于缺少 google-services.json 的构建豁免,这没关系。
    4. 从 Firebase 控制台再次下载 google-services.json 并再次放入项目并同步。
    5. 构建并运行。

    希望对某人有所帮助

    【讨论】:

      【解决方案3】:

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

      无论如何,错误信息具有误导性。

      【讨论】:

        【解决方案4】:

        在我的情况下,解决方案是在 Android Studio 中创建一个新的模拟器并在那里运行应用程序

        【讨论】:

          【解决方案5】:

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

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

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

              我最近遇到了这个问题,我尝试删除并重新安装模拟器仍然没有骰子。因此,我决定捕获错误并在存在特定错误时重试查询。如果用户真的离线,我会包含一个超时,如果不是,它将在重试后检索数据。

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

              【讨论】:

                【解决方案8】:

                访问 firebase 网站,然后创建一个数据库,然后一切顺利:)

                【讨论】:

                  猜你喜欢
                  • 2020-07-12
                  • 2022-07-01
                  • 1970-01-01
                  • 2012-04-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-09-28
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多