如何跳过初始数据并仅触发Firestore Firebase中的新更新? [英] How to skip initial data and trigger only new updates in Firestore Firebase?

查看:229
本文介绍了如何跳过初始数据并仅触发Firestore Firebase中的新更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处搜寻都没有运气。我想查询Firestore以获取所有用户WHERE类型为admin。类似于:

I've searched everywhere with no luck. I want to query Firestore to get all users WHERE type is admin. Something like:

SELECT * FROM users WHERE type=admin

当属性总计正在变化时。如果我正在使用:

but only when the property total is changing. If I'm using:

users.whereEqualTo("type", "admin").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirebaseFirestoreException e) {
        for (DocumentChange dc : snapshots.getDocumentChanges()) {
            switch (dc.getType()) {
                case ADDED:
                    //Not trigger
                    break;
                case MODIFIED:
                    //Trigger
                    break;
                case REMOVED:
                    //
                    break;
            }
        }
    }
});

在我查询时以及 total <时,第一次触发ADDED的情况/ code>已更改案例MODIFIED 再次被触发(这就是想要的)。我只想要更改而不是所有初始数据,我不需要它。如何获得?

The case ADDED is triggered first time when I query and when the total is changed case MODIFIED is triggered again (this is what is want). I want only changes and not the all initial data, I don't need it. How to get it?

请帮助我,这是我项目的最后一部分。如何跳过 case ADDED

Please help me, is the last part of my project. How to skip is case ADDED?

推荐答案

当你在听使用Firestore Query的 addSnapshotListener()方法,它:

When you are listening for changes in Cloud Firestore for realtime changes, using Firestore Query's addSnapshotListener() method, it:

开始收听此查询。

这基本上意味着第一次附加监听器时,你获取与该特定查询对应的所有文档。此外,每次文档中的属性发生更改时,都会根据该更改通知您。显然,只有当监听器保持活动状态并且未被删除时才会发生这种情况。

Which basically means that first time you attach the listener, you get all documents that correspond to that particular query. Furthermore, everytime a property within a document changes, you are notified according to that change. Obviously, this is happening only if the listener remains active and is not removed.

不幸的是,Firestore监听器不能以这种方式工作,所以你不能跳过这个案例ADDED 。您可以做的是在每个用户对象下添加一个日期属性( 这个 是如何添加它的)并在客户端上查询数据库,根据这个新属性自上一次以来发生了变化。

Unfortunately, Firestore listeners don't work that way, so you cannot skip that "case ADDED". What you can do instead, is to add add under each user object a Date property (this is how you can add it) and query your database on client, according to this new property, for all documents that have changed since a previous time.

根据Nick Cardoso的评论,未来的访问者可能会问为什么会出现这种情况,这是因为他在评论中提到的原因。我还建议看看Doug Stevenson的回答发布,以便更好地理解。

According to Nick Cardoso's comment, for future visitors that might ask why this behaviour happens, is because the reason he mentioned in his comment. I also recommend see Doug Stevenson's answer from this post, for a better understanding.

这篇关于如何跳过初始数据并仅触发Firestore Firebase中的新更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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