作者:蒋小宁蒋小羊 | 来源:互联网 | 2023-06-21 14:33
Imrunningtwotestsinfirebase.Oneversionpasses,theotherdoesnot.Thechild_removedcallbac
I'm running two tests in firebase. One version passes, the other does not. The child_removed callback works fine if it's attached before the child is actually added. It does not fire when attached after the insert.
我在firebase中运行了两个测试。一个版本通过,另一个版本不通过。如果在实际添加子项之前附加了child_removed回调,则它可以正常工作。插入后连接时不会触发。
Version that passes the test:
通过测试的版本:
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);
});
});
Version that fails the test:
未通过测试的版本:
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);
});
});
The only difference is where the child_removed event is attached, but both are attached before the data is actually deleted. In both cases, the data is indeed removed from firebase.
唯一的区别是附加了child_removed事件,但在实际删除数据之前都附加了这两个事件。在这两种情况下,确实从firebase中删除了数据。
Is there a race condition when adding the child_removed listener?
添加child_removed监听器时是否存在竞争条件?
1 个解决方案