【问题标题】:Firestore Failed to get document because the client is offlineFirestore 无法获取文档,因为客户端处于脱机状态
【发布时间】:2018-09-20 18:40:50
【问题描述】:

我在离线时遇到错误。

我已经将 Persistence 设置为 True 并在离线时获取了一些缓存数据,但是有一个函数会给我这个离线错误。 logcat 将我指向task.getResult().exists()

我不知道该怎么办,谁能帮帮我?

错误: com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.firestore.FirebaseFirestoreException: 获取文档失败,因为客户端离线。

firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {

                    if(!task.getResult().exists()){ //I GET ERROR HERE

                        Map<String, Object> likesMap = new HashMap<>();
                        likesMap.put("timestamp", FieldValue.serverTimestamp());

                        firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).set(likesMap);

                    } else {

                        firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).delete();

                    }

                }
            });

【问题讨论】:

  • 仅供参考,每当您使用任务时,您都应该确保检查任务是否成功完成,或者是否有错误。您现在的代码盲目地假设任务成功(它没有)。如果您要在 onComplete 上进行,您应该检查任务的成功或失败。或者,您可以将其拆分为根据任务状态调用的 onSuccess 和 onFailure 回调。
  • 但无论如何,听起来您请求的文档实际上并不在本地缓存中。

标签: android google-cloud-firestore


【解决方案1】:

在 Flutter android 应用开发过程中对我来说

  • 打开android studio emulator并选择擦除数据
  • 然后再次运行

注意:我只知道安卓系统。

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 2022-07-01
    • 2022-01-17
    • 1970-01-01
    • 2018-03-19
    • 2016-02-07
    • 2022-08-11
    • 2019-06-07
    相关资源
    最近更新 更多