简体   繁体   中英

Firebase Realtime Database Property Listener

I have some data like so: 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. 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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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