firestore如何检查是否添加了与特定查询匹配的新文档 [英] firestore how to check if new documents added matching a particular query

查看:36
本文介绍了firestore如何检查是否添加了与特定查询匹配的新文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我可以使用 onSapshot()代替 get()进行查询,并听取其更改(添加,删除,修改)询问.但是,当有更改时,onSnaphot()会返回多个完整文档,无论是已修改,删除还是添加的(新快照).我想要做的是,检查是否已添加与我的查询匹配的新文档,并向用户显示有新记录可用的通知,并且只有单击按钮时,它们才能够获取并查看新记录.记录.发生更改时,我不想从Firestore中获取整个文档集.我只想了解这些更改并按需获取.并仅获取添加的内容.

As I know that, instead of get() I can use onSapshot() for my queries and listen to the changes(additions, deletions, modifications) for that query. But when there are changes, onSnaphot() returns multiple full documents whether modified, deleted or added (a new snapshot). What I want to do is, to check if new documents have been added matching my query and show a notification to the user that there are new records available, and only when a button is clicked, they should be able to fetch and see the new records. I don't want to fetch from the firestore whole sets of documents when there are changes. I just want to know about those changes and fetch on demand. And fetch only the added ones.

我该怎么做?有任何想法吗?

How can I do that? Any ideas?

顺便说一句,我正在为我的应用程序使用react-native.

By the way, I am using react-native for my app.

P.S.-我知道,我可以定期运行查询并检查新查询中第一项的ID是否与先前查询中的第一项ID匹配,因此我可以检测到添加的记录并显示我的通知/按钮.但我正在寻找更优雅的解决方案.我认为通知应该从后端触发,而不是定期轮询后端.

P.S.2-使用云功能似乎不是一个合理的选择,因为这些查询对于我的应用程序的每个用户都是不同的.这将需要在Firestore上运行数千个功能(希望有更多功能).还是会吗?

P.S. 2 - Using cloud functions would not seem to be a logical option since these queries will be different for each user of my app. Which would require running thousands of functions (hopefully more) on the firestore. Or would it?

谢谢!

推荐答案

在Firestore API中,如果不实际检索更改的文档,就无法获取有关查询更改的通知.但是当然可以在调用 onSnapshot 回调时仅向用户显示通知,然后仅在用户选择刷新UI时显示这些文档中的实际数据.

There is no way in the Firestore API to get notified about changes to a query without actually retrieving the changed documents. But you can of course show just a notification to the user when your onSnapshot callback gets called, and then only show the actual data from those documents when the user chooses to refresh the UI.

第二点,当您使用快照侦听器时,Firestore客户端将仅在其他回调中检索修改后的文档.

On a second note, when you use a snapshot listener the Firestore client will only retrieve the modified documents on additional callbacks.

假设您为与10个文档匹配的查询附加了一个侦听器.在第一个 onSnapshot 回调中,您将获得10个文档,并需要为10次文档读取支付费用.现在说文档之一发生了变化.为此,调用 onSnapshot 回调时,您将再次看到10个文档,但对于更改的文档,仅收取1读费用.

Say you attach a listener for a query that matches 10 documents. On the first onSnapshot callback, you will get 10 documents and will be charged for 10 document reads. Now say that one of the documents changes. When your onSnapshot callback gets invoked for this, you will see 10 documents again, but will be charged only 1 read - for the document that was changed.

如果您只想处理更改,请查看

If you only want to process the changes, have a look at the documentation on viewing changes between snapshots, which contains a good example of how to do this with the docChanges() method.

这篇关于firestore如何检查是否添加了与特定查询匹配的新文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆