简体   繁体   English

Firebase 实时数据库属性监听器

[英]Firebase Realtime Database Property Listener

I have some data like so: leaderboards/board/{board id}/{child properties}我有一些这样的数据: leaderboards/board/{board id}/{child properties}

实时数据库中示例数据的屏幕截图

I have an added listener for boards, like so:我为板添加了一个侦听器,如下所示:

const boardRef = firebase.database().ref('leaderboards/board');
boardRef.on('child_added', (snapshot) => {
    // handle data
});

That part works fine.那部分工作正常。 I then later want to listen to just a specific property on a board, to see if it changes.然后我想听一个板上的特定属性,看看它是否发生了变化。 I do not want to listen to all child changes on the board, as a couple of the children can change really frequently and we don't want to be handling that much data except when needed (a certain app state).想听板上所有的孩子变化,因为几个孩子可以非常频繁地变化,我们不想处理那么多数据,除非需要(某个应用程序状态)。 So, I tried this:所以,我尝试了这个:

const endedRef = firebase.database().ref('leaderboards/board/123/ended');
endedRef.on('value', (snapshot) => {
    // handle data
});

However, when I update the 'ended' property in Firebase, the listener isn't called.但是,当我更新 Firebase 中的“结束”属性时,不会调用侦听器。 It's only called when I first add the listener (so it appears to be hooked up properly) but then not when the property is changed.它仅在我第一次添加侦听器时调用(因此它似乎已正确连接),但在属性更改时不会调用。 What's the proper way to solve this?解决这个问题的正确方法是什么?

This post is similar, but as I'm already trying a 'value' listener on the property, seems to either not be a fix or not work for my data: How to listen to a specific value change in Firebase?这篇文章是相似的,但由于我已经在该属性上尝试了一个“值”侦听器,似乎不是修复或不适用于我的数据: 如何侦听 Firebase 中的特定值变化?

Thanks for any and all help!感谢您的任何帮助!

This was related to something specific in my codebase, causing my listener removal to get triggered just after getting added.这与我的代码库中的特定内容有关,导致我的侦听器删除在添加后被触发。 So if you're having a similar issue, check where you remove the listener!因此,如果您遇到类似的问题,请检查删除侦听器的位置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM