Firebase child_added仅用于新项目 [英] Firebase child_added for new items only
问题描述
firebaseDb.child('invites')。on('child_added' snapshot => {
})
这个方法背后的想法是,来自数据库的有效载荷,仅使用一个操作通过减速器更新我的本地数据存储。
接下来,我需要监听任何新的或更新小孩的密钥邀请。
然而,现在的问题是,对于所有现有的密钥以及新增的密钥, child_added 事件将触发。我不想要这个行为,我只需要新的键,因为我有现有的数据检索。
我知道child_added通常用于这种类型的操作,但是,我希望减少触发的操作数,并因此触发。 p>
实现这一目标的最佳方式是什么?
谢谢,
我已经解决了以下问题方法。
firebaseDb.child('invites')。limitToLast(1).on('child_added',cb)
firebaseDb.child('invites')。on('child_changed',cb)
limitToLast (1)获取邀请的最后一个子对象,然后侦听任何新对象,将快照对象传递给cb回调。
child_changed 监听任何子更新的邀请,将快照传递给cb
I am using Firebase and Node with Redux. I am loading all objects from a key as follows.
firebaseDb.child('invites').on('child_added', snapshot => {
})
The idea behind this method is that we get a payload from the database and only use one action to updated my local data stores via the Reducers.
Next, I need to listen for any NEW or UPDATED children of the key invite. The problem now, however, is that the child_added event triggers for all existing keys, as well as newly added ones. I do not want this behaviour, I only require new keys, as I have the existing data retrieved.
I am aware that child_added is typically used for this type of operation, however, i wish to reduce the number of actions fired, and renders triggered as a result.
What would be the best pattern to achieve this goal?
Thanks,
I have solved the problem using the following method.
firebaseDb.child('invites').limitToLast(1).on('child_added', cb)
firebaseDb.child('invites').on('child_changed', cb)
limitToLast(1) gets the last child object of invites, and then listens for any new ones, passing a snapshot object to the cb callback.
child_changed listens for any child update to invites, passing a snapshot to the cb
这篇关于Firebase child_added仅用于新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!