简体   繁体   English

如何仅从 Firebase 获取没有现有数据的新数据?

[英]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):

  • Using observeSingleEvent to get the complete data set使用observeSingleEvent获取完整的数据集
  • Then returned it in reversed order by reversed()然后通过reversed()以相反的顺序返回它
  • Get the last timestamp by for example 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.

相关问题 如何通过本机反应将新字段数据更新到现有文档而不覆盖firebase-9中的现有数据 - How can update new Field data to existing document without overwrite to existing data in firebase-9 by react native 如何将新数据添加到Firebase数据库中的现有数据? - How can I add new data to existing data in a firebase database? 如何在不使用此获取按钮的情况下将数据从 firebase 检索到网络? - How to Retrieve data from firebase to web without using this get button? 如何在 Vue.js 和 Firebase 中向现有用户添加新数据 - How to add new data to existing user in Vue.js and Firebase Firebase:一次获取现有数据,然后使用child_added获取新数据 - Firebase: fetch existing data once, then use child_added to get new data 如何在不生成新密钥的情况下将数据推送到Firebase - How to push data to firebase without generating new key 没有发布键,如何将新数据添加到Firebase? - How can a new data be added to firebase without the post keys? 如何在没有唯一键的情况下在 firebase 中添加新数据 - How to add new data in firebase without the unique keys 如何从Firebase数据库获取离子数据? - How to get data from Firebase database to ionic? 如何从Firebase数据库中的对象获取数据? - How to get data from an object in Firebase database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM