如何仅从 Firebase 获取没有现有数据的新数据? [英] How to only get new data without existing data from a Firebase?

查看:27
本文介绍了如何仅从 Firebase 获取没有现有数据的新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Firebase 中有一个节点,不断更新日志文件中的信息.节点是 lines/ 并且 lines/ 的每个子节点都来自 post() 所以它有一个唯一的 ID.

I have a node in Firebase getting continually updated with information from a logfile. The node is lines/ and each child of lines/ is from a post() so it has a unique ID.

当客户端第一次加载时,我希望能够获取最后 X 个条目.我希望我会用 once() 做到这一点.但是,从那时起,我想将 on()child_ added 一起使用,以便我获得所有新数据.但是,child_ added 获取存储在 Firebase 中的所有数据,并且在初始设置后,只需要新的东西.

When a client first loads, I want to be able to grab the last X number of entries. I expect I'll do this with once(). From then on, however, I want to use an on() with child_added so that I get all new data. However, child_added gets all data stored in the Firebase and, after the initial setup, only want the new stuff.

我看到我可以在 on() 上添加一个 limitToLast(),但是,如果我说 limitToLast(1) 并且大量条目涌入,我的应用程序还会获得所有新条目吗?有没有其他方法可以做到这一点?

I see that I can add a limitToLast() on the on(), but, if I say limitToLast(1) and a flood of entries come in, will my app still get all the new entries? Is there some other way to do this?

推荐答案

您需要包含 timestamp 属性并运行查询.

You need to include a timestamp property and run a query.

// 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 返回的内容.

The Firebase SDK has methods for ordering, orderByChild() and methods for creating a range startAt(). When you combine the two you can limit what comes back from Firebase.

这篇关于如何仅从 Firebase 获取没有现有数据的新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆