简体   繁体   English

ChildEventListener和ValueEventListener Firebase接口有什么区别?

[英]What is the difference between ChildEventListener and ValueEventListener Firebase interfaces?

文档说他们都在Firebase数据库位置收听更改。

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 . 他们这样做几乎同样的事情,虽然ChildEventListener可有时更灵活:与ChildEventListener可以指定4点的动作(不同的行为onChildAddedonChildChangedonChildMovedonChildRemoved ),而ValueEventListener仅提供onDataChanged

Also ChildEventListener provides DataSnapshots (immutable copies of the data) at child's location while ValueEventListener provides a DataSnapshot of a whole node. ChildEventListener还在子位置提供DataSnapshots(数据的不可变副本),而ValueEventListener提供整个节点的DataSnapshot。

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. 只有在特定值更改时才会触发ValueEventListener,但ChildEventListener不仅会侦听该节点的值,还会侦听树的所有子节点。 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. 当此节点更改时,将触发ValueEventListener,但也会触发更新子级值的ChildEventListener。 Documentation says, that you should use ChildEventListener with caution - it can be triggered a lot of times. 文档说,你应该谨慎使用ChildEventListener - 它可以被触发很多次。

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: 1)关于新条目:

  • a)ChildEventListener.onChildAdded will be called once but the datasnapshot will only have the one added a)ChildEventListener.onChildAdded将被调用一次,但数据快照将只添加一个
  • b) ValueEventListener.onDatachange will be called once but the datasnapshot will have everybody b)ValueEventListener.onDatachange将被调用一次,但数据快照将拥有所有人

2) because of a) getting the new record in childeventlistener is 2)因为a)在childeventlistener获取新记录是

 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 因为b)在ValueEventListener中获取新记录是

    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. 因此,例如,如果您不关心当某人删除ValueEventListener中的第一个时发生的情况,您总会得到通知,但在ChildEventListener中,只有覆盖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. 您只关心新邮件,不想再在聊天室中重新插入所有邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM