ValueEventListener vs ChildEventListener for RecyclerView in Android

后端 未结 4 872
暖寄归人
暖寄归人 2020-12-15 18:06

Firebase Database users know that there are two basic listeners for listening Data: ValueEventListener and ChildEventListener. It works great when

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 18:25

    When working with collection data on Firebase you should be using:

    • onChildAdded
    • onChildChanged
    • onChildRemoved
    • onChildMoved

    You may not need all of these but I've found that usually onChildAdded and onChildRemoved are often essential. You will have to sort and keep track of the data changing in your adapter and pass that back to the RecyclerView on every updated child.

    In contrast, you can use the ValueEventListener and simply update the entire list however like you said, it would mean every update in that list will cause all the objects in the collection to be sent to you which will cost your user data and cost you more bandwidth on Firebase. This isn't recommended if your data will be updating often.

    Source

提交回复
热议问题