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

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

问题描述

我到处搜索都没有运气.我想查询 Firestore 以获取所有类型为 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

当属性 total 发生变化时.如果我正在使用:

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

案例 ADDED 在我查询时第一次触发,当 total 更改时 case 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?

推荐答案

当您在 Cloud Firestore 中监听更改以获取实时更改时,使用 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 侦听器不能那样工作,因此您不能跳过case ADDED".您可以做的是在每个用户对象下添加一个 Date 属性( 是您可以添加它的方法)并根据此新属性在客户端查询您的数据库,以查找自上次以来已更改的所有文档时间.

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 的评论,对于未来可能会问为什么会发生这种行为的访问者,是因为他在评论中提到的原因.我还建议从这个 post,以便更好地理解.

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天全站免登陆