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

问题描述

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



我知道 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(需要进一步说明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:50