Firebase事件保证子添加子删除

Firebase event guarantee child added child removed

本文关键字:添加 删除 事件 Firebase      更新时间:2023-09-26

在Firebase文档中引用:

在修改子节点时触发child_changed事件。这包括对子节点的后代的任何修改。它通常与child_addedchild_removed一起使用,以响应项列表的变化。

现在我有了这样的结构:

parent-node: {
    child1,
    child2,
    ...
}

和I按照以下顺序将侦听器附加到父节点:

parentRef.on('child_added', function() {
// Record the children to my memory cache
}
parentRef.on('child_removed', function() {
// Remove the deleted children from my memory cache
}
parentRef.on('child_changed', function() {
// Do something with the children in my memory cache
}

这是否意味着如果像上面那样注册侦听器,我就没有机会错过对子对象的任何更改?

侦听器是否同时由Firebase注册?或者说,如果在注册child_addedchild_removed之间有一个轻微的延迟,这样在注册child_removed侦听器之前有一个子删除事件,那么child_removed侦听器是否能够捕获该子删除事件?

编辑

侦听器应该基于答案和firebase文档中的另一个引用来工作:

事件最终总是反映数据的正确状态,即使在局部操作或定时导致临时差异的情况下,例如网络连接的临时丢失。

事件几乎是同时注册的。这取决于您到远程主机的延迟有多快,但它不应该干扰您的意图。