取消父项的监听器是否适用于 Firebase 中的子项
Unlink listener for parent does it applied to children in Firebase
我正在使用 firebase 在实时应用程序中同步数据。经过一番处理后,我想取消链接所有添加的听众。所以我把
myRef.off();
但是我发现有些听众还是很依恋的
我的问题是:当您关闭父节点的监听器时,它会传播到子节点还是我应该分别关闭每个级别?
Detach a callback previously attached with on(). Note that if on() was called multiple times with the same eventType and callback, the callback will be called multiple times for each event, and off() must be called multiple times to remove the callback.
这来自关于 off() 方法的 firebase 文档 (link)。
因此,如果我没看错,你必须为你执行的每个 on() 调用 off()。
我们来试试吧。
ref.on("value", function(snapshot) {
console.log("parent: "+JSON.stringify(snapshot.val()));
});
ref.child("child").on("value", function(snapshot) {
console.log("child: "+JSON.stringify(snapshot.val()));
});
ref.set('1');
ref.child('child').set('2');
ref.off();
ref.child('child').set('3');
ref.child('child').off();
ref.set('4');
输出:
parent: "1"
child: "2"
parent: {"child":"2"}
child: "3"
因此在 parent 侦听器上调用 off
之后,child 侦听器仍然会触发 ("3"
)。但是,如果我们得到相同的 child 并调用 off
,它就不再适用了 ("4"
)。
JSBin: http://jsbin.com/wutaza/edit?js,console
结论:off() 不会从 child 个节点中删除侦听器。
多亏了这个线程中的帖子,我才能够弄清楚如何停止我的回调,这些回调在为传递给它们的预期 Firebase 调用进行初始调用后一直触发。当我直接在控制台中更新时,甚至会发生这种意外的回调。
我得到了以下解决方案来在回调运行后脱离监听器:
var queryRef = someItemRef.limitToLast(1);
queryRef.on('child_added', function (data) {
queryRef.off();
onAddSomeItemReadyFunc(data);
});
For me - understanding the listener logic and avoiding "rogue callbacks" - has been a trickier aspect of working with Firebase DB. But we're gettn' there :)
@Frank van Puffelen,再次感谢您提供好的注释和示例,帮助我们的 Firebase 客户端开发人员重回正轨!
我正在使用 firebase 在实时应用程序中同步数据。经过一番处理后,我想取消链接所有添加的听众。所以我把
myRef.off();
但是我发现有些听众还是很依恋的
我的问题是:当您关闭父节点的监听器时,它会传播到子节点还是我应该分别关闭每个级别?
Detach a callback previously attached with on(). Note that if on() was called multiple times with the same eventType and callback, the callback will be called multiple times for each event, and off() must be called multiple times to remove the callback.
这来自关于 off() 方法的 firebase 文档 (link)。
因此,如果我没看错,你必须为你执行的每个 on() 调用 off()。
我们来试试吧。
ref.on("value", function(snapshot) {
console.log("parent: "+JSON.stringify(snapshot.val()));
});
ref.child("child").on("value", function(snapshot) {
console.log("child: "+JSON.stringify(snapshot.val()));
});
ref.set('1');
ref.child('child').set('2');
ref.off();
ref.child('child').set('3');
ref.child('child').off();
ref.set('4');
输出:
parent: "1"
child: "2"
parent: {"child":"2"}
child: "3"
因此在 parent 侦听器上调用 off
之后,child 侦听器仍然会触发 ("3"
)。但是,如果我们得到相同的 child 并调用 off
,它就不再适用了 ("4"
)。
JSBin: http://jsbin.com/wutaza/edit?js,console
结论:off() 不会从 child 个节点中删除侦听器。
多亏了这个线程中的帖子,我才能够弄清楚如何停止我的回调,这些回调在为传递给它们的预期 Firebase 调用进行初始调用后一直触发。当我直接在控制台中更新时,甚至会发生这种意外的回调。
我得到了以下解决方案来在回调运行后脱离监听器:
var queryRef = someItemRef.limitToLast(1);
queryRef.on('child_added', function (data) {
queryRef.off();
onAddSomeItemReadyFunc(data);
});
For me - understanding the listener logic and avoiding "rogue callbacks" - has been a trickier aspect of working with Firebase DB. But we're gettn' there :)
@Frank van Puffelen,再次感谢您提供好的注释和示例,帮助我们的 Firebase 客户端开发人员重回正轨!