[英]How to only get new data without existing data from a Firebase?
I have a node in Firebase getting continually updated with information from a logfile.我在 Firebase 中有一个节点,不断更新日志文件中的信息。 The node is lines/
and each child of lines/
is from a post()
so it has a unique ID.该节点是lines/
和的每个子lines/
是从post()
从而它具有一个唯一的ID。
When a client first loads, I want to be able to grab the last X
number of entries.当客户端第一次加载时,我希望能够获取最后X
个条目。 I expect I'll do this with once()
.我希望我会用once()
做到这一点。 From then on, however, I want to use an on()
with child_added
so that I get all new data.但是,从那时起,我想使用on()
和child_added
来获取所有新数据。 However, child_added
gets all data stored in the Firebase and, after the initial setup, only want the new stuff.但是, child_added
获取存储在 Firebase 中的所有数据,并且在初始设置后,只需要新的东西。
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?我看到,我可以添加一个limitToLast()
上on()
但是,如果我说limitToLast(1)
和条目的洪水回来了,我的应用程序仍然可以得到所有新项目? Is there some other way to do this?有没有其他方法可以做到这一点?
You need to include a timestamp
property and run a query.您需要包含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
});
The Firebase SDK has methods for ordering, orderByChild()
and methods for creating a range startAt()
. Firebase SDK 具有排序方法orderByChild()
和创建范围startAt()
。 When you combine the two you can limit what comes back from Firebase.当您将两者结合起来时,您可以限制从 Firebase 返回的内容。
I think there is a problem in @David East's solution.我认为@David East 的解决方案存在问题。 He is using the local timestamp which may cause problem if the time is not accurate in client device.他正在使用本地时间戳,如果客户端设备中的时间不准确,这可能会导致问题。 Here is my suggested solution (iOS Swift):这是我建议的解决方案(iOS Swift):
observeSingleEvent
to get the complete data set使用observeSingleEvent
获取完整的数据集reversed()
然后通过reversed()
以相反的顺序返回它data[0].timestamp
通过例如data[0].timestamp
获取最后一个时间戳Using queryStarting
for timestamp使用queryStarting
作为时间戳
self._dbref.queryOrdered(byChild: "timestamp").queryStarting(atValue: timestamp+1) .observe(.childAdded, with: { snapshot in print(snapshot.value) })
You have the right idea.你有正确的想法。 child_added
should be called only for the new nodes. child_added
应该只为新节点调用。 Without source code it's hard to tell why you get all the data in your child_added
event.如果没有源代码,很难说出为什么在child_added
事件中获得所有数据。
You can check the chat demo app to see how they load new chat messages.您可以查看聊天演示应用程序以了解它们如何加载新的聊天消息。 The use case sounds similar.用例听起来很相似。
https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L347 https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L347
Here's temporary but quick solution:这是临时但快速的解决方案:
// define a boolean
var bool = false;
// fetch the last child nodes from firebase database
ref.limitToLast(1).on("child_added", function(snap) {
if (bool) {
// all the existing child nodes are restricted to enter this area
doSomething(snap.val())
} else {
// set the boolean true to doSomething with newly added child nodes
bool = true;
}
});
Disadvantage: It will load all the child nodes.缺点:会加载所有的子节点。
Advantage: It will not process existing child nodes but just the newly added child nodes.优点:它不会处理现有的子节点,而只会处理新添加的子节点。
limitToLast(1)
will do the work. limitToLast(1)
将完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.