如何放弃Firebase数据库中的初始数据 [英] how to discard initial data in a Firebase DB

查看:141
本文介绍了如何放弃Firebase数据库中的初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个简单的应用程序,通知客户端其他客户端单击了一个按钮。我将点击数据存储在Firebase( db )中:

  db.push({消息:数据});所有的客户都会收到其他用户的点击通知  

$ $ $ $ $ $ c $ db.on('child_added',function(snapshot){
var msg = snapshot.val()。msg;
});

但是,当页面第一次加载时,我想放弃堆栈上的任何现有数据。我的策略是在定义 db.on('child_added',...) db.once()为了得到最初的孩子数量,然后用它来放弃对 db.on('child_added',...)的调用次数。 / p>

不幸的是,所有对db.on('child_added',...)的调用都是在之前发生的 得到初始计数,所以失败。



我怎样才能有效地简单地丢弃最初的数据?

解决方案

如果我正确地理解了你的问题,听起来好像你只需要 希望从用户访问页面以来添加的数据。在Firebase中,您所描述的行为是有意设计的,因为数据总是在变化,并且没有旧数据和新数据的概念。



但是,如果您只想显示在页面加载后添加的数据,请尝试先忽略所有事件,直到完整的一组子项至少加载一次。例如:

  var ignoreItems = true; 
var ref = new Firebase('https://< your-Firebase> .firebaseio.com');
ref.on('child_added',function(snapshot){
if(!ignoreItems){
var msg = snapshot.val()。msg;
// do something这里
}
});
ref.once('value',function(snapshot){
ignoreItems = false;
});

这种方法的替代方法是将优先级写入新的项目,是 Firebase.ServerValue.TIMESTAMP (当前服务器时间),然后使用 .startAt(...)使用当前时间戳进行查询。但是,这比上面描述的方法要复杂得多。

I'm making a simple app that informs a client that other clients clicked a button. I'm storing the clicks in a Firebase (db) using:

db.push({msg:data});

All clients get notified of other user's clicks with an on, such as

db.on('child_added',function(snapshot) { 
  var msg = snapshot.val().msg; 
});

However, when the page first loads I want to discard any existing data on the stack. My strategy is to call db.once() before I define the db.on('child_added',...) in order to get the initial number of children, and then use that to discard that number of calls to db.on('child_added',...).

Unfortunately, though, all of the calls to db.on('child_added',...) are happening before I'm able to get the initial count, so it fails.

How can I effectively and simply discard the initial data?

解决方案

If I understand your question correctly, it sounds like you only want data that has been added since the user visited the page. In Firebase, the behavior you describe is by design, as the data is always changing and there isn't a notion of "old" data vs "new" data.

However, if you only want to display data added after the page has loaded, try ignoring all events prior until the complete set of children has loaded at least once. For example:

var ignoreItems = true;
var ref = new Firebase('https://<your-Firebase>.firebaseio.com');
ref.on('child_added', function(snapshot) {
  if (!ignoreItems) {
    var msg = snapshot.val().msg;
    // do something here
  }
});
ref.once('value', function(snapshot) {
  ignoreItems = false;
});

The alternative to this approach would be to write your new items with a priority as well, where the priority is Firebase.ServerValue.TIMESTAMP (the current server time), and then use a .startAt(...) query using the current timestamp. However, this is more complex than the approach described above.

这篇关于如何放弃Firebase数据库中的初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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