Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ChildEventListener and ValueEventListener Firebase interfaces?

The documentation says they both listen to changes at a Firebase database location.

like image 358
Gil Avatar asked Nov 23 '16 12:11

Gil


People also ask

What is ChildEventListener?

ChildEventListener : A ChildEventListener listens for changes to the children of a specific database reference, for example the root node of a database.

What is addListenerForSingleValueEvent?

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.

What is firebase database reference?

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.

How do I get rid of value event listener?

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.


2 Answers

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.

like image 189
Margarita Litkevych Avatar answered Sep 21 '22 05:09

Margarita Litkevych


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.

like image 31
Alex Shutov Avatar answered Sep 23 '22 05:09

Alex Shutov