Firebase价值和ChildAdded(需要进一步说明) [英] Firebase Value and ChildAdded (further explanation needed)

查看:241
本文介绍了Firebase价值和ChildAdded(需要进一步说明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一遍又一遍地阅读了Firebase文档,只是需要一些说明。这不是关于我的特定代码,而是我对Firebase的一般理解。



我知道 .Value 每次调用时都会检索所有数据,并继续监视数据库的更改。并且 .ChildAdded 查找引用的子节点中的更改。 .ChildAdded 是否也检索最初的所有值?我应该为相同的路径实现 .Value .ChildAdded 吗?如果没有,当我试着做 .ChildAdded 时,什么也没有显示,但是当我运行 .Value 时,我所有的项目像他们应该显示的那样。我不想要的是每次更改或添加一个值时从数据库中重新获取每个值,因为当我有数千个项目要获取时,似乎会导致一些带宽问题。最好的工作流是用ObserveSingleEventOfType创建一个.Value函数( getAllItems()),所以它只调用一次,然后有一个重复函数( getNewItems()),除了 .ChildAdded 和observeEventOfType?

解决方案<如果你有这个节点

$ $ p $ $ $ $ $ $ $ node $ {
data1:data,
data2:{
subdata1:1,
subdata2:2
}
}



值 with observeEventOfType 将被调用第一次+ code>node changes, Value with observeSingleEventOfType 一次

Child_Added 与observeEventOfType将被调用两次[data1(它将包含字符串data和data2(它将包含一个包含子元素subdata1subdata2 ]



Child_Changed with observeEventOfType 每一次data1data2 changed(data2 / code>将会每次调用subdata1subdata2更改,或者如果您插入一个subdata3

这可以组合使用,但它取决于每个场景... p>

对于事件类型:



通常当您指向最后一个节点,比如 data1你使用 Value ,当你在一个节点有一个类似的节点列表data2 / code>您使用 child_added child_changed child_removed <对于观察者类型:

如果你想得到值具体时刻你使用单一事件观察者。如果你想随时注意价值,你可以使用事件观察者

I've read the Firebase documentation over and over and again and just need some clarification. This isn't regarding my specific code as much as my understanding of Firebase in general.

I know that .Value retrieves all the data each time its called and continues to monitor the database for changes. And .ChildAdded looks for changes in the children of the reference. Does .ChildAdded also retrieve all of the values initially? Should I be implementing both .Value and .ChildAdded for the same path? If not, when I try just doing .ChildAdded, nothing shows up but when I run .Value, all of my items show up like they are supposed to. What I don't want is for every value to be re-fetched from the database each time a single value is changed or added because that would seemingly cause some bandwidth issues later when I have thousands of items to fetch. Would be best workflow be to create a .Value function (getAllItems()) with ObserveSingleEventOfType so it only calls it once and then have a duplicate function (getNewItems()) except with .ChildAdded and observeEventOfType?

解决方案

if you have this node

 node:{
   data1:"data",
   data2:{
     subdata1:1,
     subdata2:2
   }
 }

Value with observeEventOfType will be called the first time + every time something inside "node" changes , Value with observeSingleEventOfType will be called only once

Child_Added with observeEventOfType will be called twice ["data1"(it will contain the string "data" and "data2"(it will contain an object with the childs "subdata1" and "subdata2"]

Child_Changed with observeEventOfType will be called every time "data1" or "data2" changed (an event in "data2" will be called every time "subdata1" or "subdata2" change, or if you insert a "subdata3")

This can be used in combination but it depends in each scenario...

FOR THE EVENT TYPE:

usually when you are pointing at a final node like "data1" you use Value and when you are in a node that has a list of similar nodes like "data2" you use a combination of child_added, child_changed and child_removed

FOR THE OBSERVER TYPE:

it's straightforward if you want to get the value at a specific moment; you use the "single event observer". If you want to keep an eye of the value over time you use "event observer"

这篇关于Firebase价值和ChildAdded(需要进一步说明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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