【发布时间】:2016-12-07 04:24:49
【问题描述】:
我在 firebase 中运行两个测试。一个版本通过,另一个没有。如果在实际添加孩子之前附加了 child_removed 回调,则它可以正常工作。插入后连接时不会触发。
通过测试的版本:
it("A) listens to child_removed raw (Before Insert)", function (done) {
this.timeout(5000);
let ref = DataServices.database.ref();
let pushRef = ref.child('test/testChildRemovedA').push();
let newKey = pushRef.key;
let insertPath = 'test/testChildRemovedA/' + newKey;
let callback = (snapshot) => { done(); }
DataServices.database.ref('test/testChildRemovedA/').on("child_removed", callback);
let updates = {};
updates[insertPath] = 'hi';
DataServices.database.ref().update(updates).then(() => {
updates[insertPath] = null;
DataServices.database.ref().update(updates);
});
});
测试失败的版本:
it("B) listens to child_removed raw (After Insert)", function (done) {
this.timeout(10000);
let ref = DataServices.database.ref();
let pushRef = ref.child('test/testChildRemovedB').push();
let newKey = pushRef.key;
let insertPath = 'test/testChildRemovedB/' + newKey;
let callback = (snapshot) => { done(); }
let updates = {};
updates[insertPath] = 'hi';
DataServices.database.ref().update(updates).then(() => {
DataServices.database.ref('test/testChildRemovedB/').on("child_removed", callback);
updates[insertPath] = null;
DataServices.database.ref().update(updates);
});
});
唯一的区别是 child_removed 事件的附加位置,但两者都是在实际删除数据之前附加的。在这两种情况下,数据确实已从 firebase 中删除。
添加 child_removed 侦听器时是否存在竞争条件?
【问题讨论】:
标签: javascript firebase firebase-realtime-database