我在 Firebase 中有一个节点不断使用日志文件中的信息进行更新。该节点是lines/
和每个孩子lines/
是来自一个post()
所以它有一个唯一的ID。
当客户端第一次加载时,我希望能够获取最后一个X
条目数。我希望我会这样做once()
。然而从那时起,我想用一个on()
with child_added
这样我就可以获得所有新数据。然而,child_added
gets all数据存储在 Firebase 中,并且在初始设置后,只需要新的内容。
我发现我可以添加一个limitToLast()
on the on()
,但是,如果我说limitToLast(1)
大量条目涌入,我的应用程序还会获得所有新条目吗?还有其他方法可以做到这一点吗?
您需要包括一个timestamp
属性并运行查询。
// Get the current timestamp
var now = new Date().getTime();
// Create a query that orders by the timestamp
var query = ref.orderByChild('timestamp').startAt(now);
// Listen for the new children added from that point in time
query.on('child_added', function (snap) {
console.log(snap.val()
});
// When you add this new item it will fire off the query above
ref.push({
title: "hello",
timestamp: Firebase.ServerValue.TIMESTAMP
});
Firebase SDK 具有排序方法,orderByChild()
以及创建范围的方法startAt()
。当您将两者结合起来时,您可以限制从 Firebase 返回的内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)