Google FireStore QuerySnapshot没有返回新添加的文档?

huangapple go评论56阅读模式
英文:

Google FireStore QuerySnapshot not returning newly added documents?

问题

为了学习目的,我创建了一个Android应用程序,并尝试从Google Firestore获取文档,一开始运行良好,我能够读取数据,但后来当我在Firestore控制台中添加了更多文档后,代码仍然返回给我旧记录。新添加的文档没有在QuerySnapshot中返回。我怀疑可能存在缓存问题,或者是否有任何设置我应该更改以读取最新文档或避免缓存记录?
有什么建议吗?

以下是我考虑阅读文档的代码片段 -

db.collection("posts")
    .get()
    .addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            QuerySnapshot querySnapshot = task.getResult();

            // 在快照中遍历文档
            for (QueryDocumentSnapshot document : querySnapshot) {
                  String title = document.getString("title");
                  // 在日志中打印标题
             }
           } else {
                 // 其他情况
                }
            });
英文:

For learning purposes, I created an android app and tried to fetch documents from Google Firestore, it worked fine and I was able to read data but later, when I added more documents in the Firestore console, the code is running but still returning me old records. Newly added documents are not returned by QuerySnapshot. I doubt, if it could be any cache issue or if is there any setting that I should change to read the latest documents or to avoid cached records?
any suggestion?

Below is the code snippet, I was considering reading the Documents -

            db.collection("posts")
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        QuerySnapshot querySnapshot = task.getResult();

                        // Iterate over the documents in the snapshot
                        for (QueryDocumentSnapshot document : querySnapshot) {
                              String title = document.getString("title");
                              // printing title in logs
                         }
                       } else{
                             // some else part
                            }
                        });

答案1

得分: 2

新添加的文档不会在QuerySnapshot中返回。

这是预期的行为。正如@DougStevenson在他的评论中提到的,当您使用Query#get()时,这是不可能的。这个方法只会一次性读取数据。这意味着您不会收到有关服务器上发生的任何更改的通知。您所寻找的是一个实时监听器。在代码中,这将是:

db.collection("posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot value,
                        @Nullable FirebaseFirestoreException e) {
        if (e != null) {
            Log.w(TAG, "监听失败。", e);
            return;
        }

        for (QueryDocumentSnapshot doc : value) {
            String title = document.getString("title");
            if (title != null) {
                Log.d(TAG, title);
            }
        }
    }
});
英文:

> Newly added documents are not returned by QuerySnapshot.

That's the expected behavior. As @DougStevenson mentioned in his comment, that's not possible when you're using Query#get(). This method reads that data only once. This means that you aren't notified about any change that is happening on the server. What you are looking for, is a real-time listener. In code that would be:

db.collection(&quot;posts&quot;).addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
            @Override
            public void onEvent(@Nullable QuerySnapshot value,
                                @Nullable FirebaseFirestoreException e) {
                if (e != null) {
                    Log.w(TAG, &quot;Listen failed.&quot;, e);
                    return;
                }

                for (QueryDocumentSnapshot doc : value) {
                    String title = document.getString(&quot;title&quot;);
                    if (title != null) {
                        Log.d(TAG, title);
                    }
                }
            }
        });

huangapple
  • 本文由 发表于 2023年7月7日 02:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定