ValueEventListener vs ChildEventListener for RecyclerView in Android

后端 未结 4 869
暖寄归人
暖寄归人 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:32

    I had the same exact problem (ValueEventListener vs ChildEventListener for RecyclerView in Android).

    The solution I used was to combine both this way :

    Suppose dbref points to a firebase db location where my posts are.

    1. Use vel (ValueEventListener) to populate the recyclerview with the list of current data at dbref. Something like this: vel = dbref.addValueEventListener(vel);
    2. Once i have the list of current data at dbref, I remove the listener vel from dbref : dbref.removeEventListener(vel);. This is not necessary if you used dbref.addListenerForSingleValueEvent(vel); in step 1.
    3. Write a Query query to filter new posts inserted at dbref from now on. Something like this: query = dbref.orderByChild(post.createdat).startAt(System.currentTimeMillis())
    4. Attach a cel (ChildEventListener) to query to receive only new posts : query.addChildEventListener(cel);

提交回复
热议问题