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?
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.
A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.
With Firebase database queries, you can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild(), orderByKey(), or orderByValue().
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.
First of all, you want to compare apples and oranges. MongoDB is a NoSQL database, whereas Firebase is a platform for mobile and web apps development that contains a lot of products and services, including Firestore which is the NoSQL database as well, so it would make sense to compare those two.
Here, dbRef is the database reference to the parent node. For each dbRef of different sports, I have a child node “contacts”. I also have a Java class made for the children of “contacts”, in order to parse the data in a specific format. So, I get DataSnapshot of the child (“contacts”), using the first highlighted line of code.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With