【问题标题】:child_removed doesn't always fire on firebasechild_removed 并不总是在 firebase 上触发
【发布时间】: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


    【解决方案1】:

    我认为您不需要任何回调。代码应该很简单。

    Retrieve Data on the Web

    child_removed 事件在直系子级被触发时触发 删除。它通常与 child_added 和 child_changed 事件。传递给事件事件的快照包含 被移除孩子的数据。

    child_changed 事件在任何时候触发子节点 修改的。这包括对孩子后代的任何修改 节点。它通常与 child_added 和 child_removed 事件以响应对项目列表的更改。这 传递给事件监听器的快照包含更新的数据 孩子。

    var commentsRef = firebase.database().ref('post-comments/' + postId);
    commentsRef.on('child_added', function(data) {
      addCommentElement(postElement, data.key, data.val().text, data.val().author);
    });
    
    commentsRef.on('child_changed', function(data) {
      setCommentValues(postElement, data.key, data.val().text, data.val().author);
    });
    
    commentsRef.on('child_removed', function(data) {
      deleteComment(postElement, data.key);
    });
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多