Firebase child_added,无需先加载所有数据 [英] Firebase child_added without loading all data first

查看:70
本文介绍了Firebase child_added,无需先加载所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在其中加载一些初始数据(用Firebase.once('value')完成),然后在以后的某个时刻,我想接收已添加到该Firebase参考中的子节点的事件.

I have an application in which I want to load some amount of initial data (accomplished with Firebase.once('value')) and then at some point later I'd like to receive events of child nodes that have been added to that Firebase reference.

为此,我想使用Firebase.on('child_added'),但是根据定义(在实践中可以看到),它首先在该Firebase引用中加载所有数据.

For this, I'd like to use Firebase.on('child_added'), but according to the definition (and seen in practice) it loads ALL of the data at that Firebase reference first.

是否有一种方法可以解决此问题,并且仅侦听child_added事件.另外,诸如转储初始数据集之类的变通方法也不是解决方案(想象一个数据集具有超过一百万个数据点-我不想每个数据点都只是在添加一个数据时监听!).

Is there a way to get around this behavior and only listen for child_added events. Also, workarounds like dumping the initial set of data are not solutions (imagine a dataset with over a million data points - I don't want to have to every data point just to listen for when one gets added!).

Firebase.on('child_added')可以与limit()组合以限制来自初始请求的数据量.对于我的应用程序而言,可能仅在一个时间点将一个数据点添加到Firebase中,因此我使用了Firebase.limit(1).on('child_added'),它限制了加载到单个数据点的初始数据量.但是,由于两个原因,我不喜欢这种解决方法:

Firebase.on('child_added') can be combined with limit() to limit the amount of data that comes from the initial request. Likely for my application, only one data point will be added to the Firebase at a point in time so I've used Firebase.limit(1).on('child_added') which limits the amount of initial data loaded to a single data point. But, I don't like this workaround for two reasons:

  1. 这仍然是一种解决方法.我仍然不得不忽略那些我不必担心忽略的数据.
  2. 如果出于某种原因,我的应用程序的性质发生了变化,并且我一次要向Firebase参考添加多个子级,是否会施加limit(1)限制应用程序接收所有已添加的子级?我不确定是否是这种情况,还是不确定是否会为添加的每个子项调用child_added,而不管它们是否成批添加.
  1. It's still a workaround. I'm still having to ignore data that I shouldn't have to worry about ignoring.
  2. If for some reason the nature of my application was to change and I was to add multiple children to the Firebase reference at one time - would imposing a limit(1) limit the application from receiving all added children? I'm not sure if this is the case or if child_added would get called for each individual child added regardless if they were added as a batch.

提供一个true/false标志作为调用的参数似乎是一个不错的解决方案,但是我将拭目以待是否有我一直在掩饰的答案...

It seems like providing a true/false flag as an argument to the call would be a nice solution, but I'll wait to see if there's an answer I've been glossing over...

推荐答案

删除旧邮件

如果您对历史数据不感兴趣,则可以将其用作消息队列.如果是这样,那么在读取旧记录后,应将其删除(或存档到备用路径).这样,问题就解决了.

If you aren't interested in historical data, then you are utilizing this as a message queue. If that is the case, than old records should be deleted (or archived to an alternate path) after reading them. In this way, the problem is self resolving.

如果要维护此模型,可以使用几个不错的选择:

If you want to maintain this model, a couple good options are available:

存储上一次读取的密钥,并将其用作下一次加载的起点

您甚至可以将密钥存储在Firebase中

You could even store the key in Firebase

var fb = new Firebase(URL);
fb.child('last_read_key').once('value', function(lastReadSnap) {
   var lastKey = lastReadSnap.val();
   var pri = lastReadSnap.getPriority();
   fb.child('data_to_read').startAt(pri, lastKey).on('child_added', function(snap) {
      if( snap.name() !== lastKey ) {
         console.log('new record', snap.name());
         fb.child('last_read_key').setWithPriority(snap.name(), snap.getPriority());
      }
   });
});

使用优先级标记旧记录的时间戳

写记录时,请使用setWithPriority:

When writing records, use setWithPriority:

fb.child('records/$recordid').setWithPriority(data, Firebase.ServerValue.TIMESTAMP);

现在,您可以读取从现在"开始的记录:

Now you can read records starting at "now":

fb.child('records').startAt(Date.now()).on('child_added', function(snap) {
    console.log('new child', snap.name());
});

请注意此处使用时间戳的一个警告.我在startAt()命令中使用了Date.now(),因为它当前不支持Firebase.ServerValue.TIMESTAMP(为此提交的错误).

Note one caveat of using timestamps here. I used Date.now() in the startAt() command, because it doesn't currently support Firebase.ServerValue.TIMESTAMP (bug filed for this).

这篇关于Firebase child_added,无需先加载所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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