The documentation says they both listen to changes at a Firebase database location.
ChildEventListener : A ChildEventListener listens for changes to the children of a specific database reference, for example the root node of a database.
addValueEventListener() keep listening to query or database reference it is attached to. But addListenerForSingleValueEvent() executes onDataChange method immediately and after executing that method once, it stops listening to the reference location it is attached to. Follow this answer to receive notifications.
A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location. This class is the starting point for all Database operations.
Detach listeners Callbacks are removed by calling the removeEventListener() method on your Firebase database reference. If a listener has been added multiple times to a data location, it is called multiple times for each event, and you must detach it the same number of times to remove it completely.
They do almost same thing, though ChildEventListener can be sometimes more flexible: with ChildEventListener you can specify different behavior for 4 actions (onChildAdded
, onChildChanged
, onChildMoved
and onChildRemoved
), while ValueEventListener provides only onDataChanged
.
Also ChildEventListener provides DataSnapshots (immutable copies of the data) at child's location while ValueEventListener provides a DataSnapshot of a whole node.
ValueEventListener gets fired only when that specific value changes, but ChildEventListener listens not only value of that node, but also for all child nodes of tree. Say, you have node, which has one children. ValueEventListener will be triggered when this node changes, but ChildEventListener will also be triggered whenewer child values is changed as well. Documentation says, that you should use ChildEventListener with caution - it can be triggered a lot of times.
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