The documentation says they both listen to changes at a Firebase database location.
3 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.

- 2,086
- 20
- 28
-
2isnt a DataSnapshot of immutable as well? So the only difference is that ChildEventListener has 4 specific methods while ValueEventListener has 1 broad method (any change) – Gil Nov 24 '16 at 02:10
-
1yep. DataSnapshot is always immutable. And you are right - if you need 1 broad method you'd rather use ValueEventListener. And ChildEventListener should be used if you have some functionality to bond to add/change/move/remove of child items. – Margarita Litkevych Nov 24 '16 at 17:57
-
1Also, this answer http://stackoverflow.com/a/34532739/2915480 relates to interesting topic of "Firebase Events Guarantee". That, again, proves your words about ValueEventListener be more broad than ChildEventLIstener. – Margarita Litkevych Nov 24 '16 at 17:58
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.

- 3,217
- 2
- 13
- 11
-
Thanks but the value event listener gets a new snapshot every time there is a change right? And you could configure the scope to be the entire database which effectively does the same thing as childeventlistener. This is my confusion – Gil Nov 23 '16 at 23:53
-
Only in Android you are forced to listen for all childEventListener events. in iOS and Web, you can listen to specific childEvents like child added. You shouldn't really use child listener at all as it is going to incur so much money for data downloaded unnecessarily. I hope the Android team fixes this and Android would be great as other platforms. – coolcool1994 May 13 '20 at 18:07
these are the key differences between the two
if your database has following records:
-LDU4T1buanVuJrpOYxW
message:"hi stack"
user: "john"
-LDasdfa1buanVuJrpOYxW
message:"hi john"
user: "stack"
1) on new entry:
- a)ChildEventListener.onChildAdded will be called once but the datasnapshot will only have the one added
- b) ValueEventListener.onDatachange will be called once but the datasnapshot will have everybody
2) because of a) getting the new record in childeventlistener is
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String,String> map =(Map) dataSnapshot.getValue();
String message = map.get("message").toString();
because of b) getting the new record in ValueEventListener is
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
.... loop until end
Map<String,String> map = (Map)data.getValue();
String message = map.get("message").toString();
so for example if you don't care what happen when somebody delete the first in ValueEventListener you always get notified but in ChildEventListener you only get notified if you override onChildRemoved.
so it depends on what you want to do. for example in a chat app. you will only care about new messages, you don't want to be reinserting all messages again in your chat room.

- 242
- 2
- 7