我怎么知道 Firebase 查询何时结束

How can I know when a Firebase query has ended

我想从我的数据存储中收集最近的 10 个项目。我相信我应该使用 .child().limitToLast() 来执行此操作,每次将结果添加到 child.

时都会发出一个事件

我不一定知道总共有没有10个项目,所以简单的计数器是行不通的。

我怎么知道 Firebase 何时完成给我结果?

示例查找代码:

var plots = [];
firebaseDatastore.child("plots").orderByChild("unicode-timestamp").limitToLast(10).on("child_added", function(snapshot) {
    // Add the new item to the list
    plots.push(snapshot.val());
});

我想知道最终剧情什么时候加完,不管有没有达到上限。

Firebase 查询永远不会真正结束。它会继续监视(在您的情况下)unicode-timestamp 的地块,并保留最后 10 个地块的 "window"。

所以:

  • child_added:时间戳1
  • child_added:时间戳2
  • ...
  • child_added:时间戳9
  • child_added:时间戳10

然后当你添加另一个情节时:

  • child_removed:时间戳1
  • child_added:时间戳11

如果您不想使用此行为,您有两个选择:

  1. 使用 value 事件,然后 snapshot.forEach 在 child 节点上
  2. 当你达到你期望的child人
  3. 人数时,
  4. 保持一个计数器和off你的听众