Firebase价值和ChildAdded(需要进一步说明) [英] Firebase Value and ChildAdded (further explanation needed)
问题描述
我知道 .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
(它将包含一个包含子元素subdata1
和subdata2
]
这可以组合使用,但它取决于每个场景... p> 对于事件类型: 通常当您指向最后一个节点,比如 如果你想得到值具体时刻你使用单一事件观察者。如果你想随时注意价值,你可以使用事件观察者 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 if you have this node 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 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屋! Child_Changed
with observeEventOfType
每一次data1
或data2
changed(data2 / code>将会每次调用
subdata1
或subdata2
更改,或者如果您插入一个subdata3
)
data1
你使用 Value
,当你在一个节点有一个类似的节点列表data2 / code>您使用
child_added
, child_changed
和 child_removed <对于观察者类型:
.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? 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"
)"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