Firebase:添加新孩子 [英] Firebase: Get New Child Added

查看:57
本文介绍了Firebase:添加新孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序启动时,我将对Firebase进行初始提取以提取所有数据.以后将数据添加到Firebase时,我只希望它获取已添加的新子项.

When my application starts I do an initial fetch to Firebase to pull down all my data. When I later add data to my Firebase, I only want it to get the new child that's been added.

我目前正在实现这样的目标.在Firebase中,有没有更好的方法或内置的方法?

I'm currently achieving this like this. Is there a better way, or a built in way to do this in Firebase?

let initialFetch = false;

ref.once('value', snap => {
  // get all child data
  initialFetch = true;  
});

ref.on('child_added', (snap) => {
  if (!initialFetch) return;
  // get new child data
});

感谢您的帮助.预先感谢!

Any help is appreciated. Thanks in advance!

推荐答案

  1. 如果同时需要新数据和后续数据,请使用child_added.它将立即为所有现有子项触发,随后为所有添加的子项触发.

  1. If you need both new data and subsequent data, use child_added. It will fire immediately for all existing children and subsequently for any child that is added.

如果您只想获取新数据,请为您的孩子添加一个时间戳,并ref.orderByChild('timestamp').startAt(Date.now())

if you only want new data, add a timestamp to your children and ref.orderByChild('timestamp').startAt(Date.now())

如果您同时需要新数据和后续数据,但又想以不同的方式对待最初的孩子,请像现在一样使用child_added value.

If you need both new data and subsequent data, but want to treat the initial children differently: use child_added and value as you do now.

另请参阅:如何只能从Firebase获取没有现有数据的新数据?

这篇关于Firebase:添加新孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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