How To Retrieve A List From Firebase Avoid Asynchronous
Solution 1:
You currently use a ChildEventListener
, which means your onChildAdded
gets called for each child node immediately and then later whenever a new child is added. This indeed can be a lot of invocations.
If you use a ValueEventListener
, its onDataChange
will only be called once for the initial data (no matter how many child nodes there are), and then once for each change.
By adding a ValueEventListener
to your current set up, you can keep things simple: add the child nodes to the lit like you're already doing, but only sort in onDataChange
.
myRef.child("User").addValueEventListener(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
sortUser(userList);
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
};
Firebase will only synchronize the data for the User
node once, even when you have two listeners on it.
Solution 2:
You should probably retrieve the data sorted server side by using order-by methods and then listen to that one.
var userRef = firebase.database().ref('posts').orderByChild('Users');
If I guess correctly, you would not need separate sorting client side.
You can also filter data. Do refer the docs
Post a Comment for "How To Retrieve A List From Firebase Avoid Asynchronous"