Skip to content Skip to sidebar Skip to footer

Arraylist Not Updating Inside Onchildadded Function

public class DataService { private static DataService ourInstance = new DataService(); private DatabaseReference mDatabase; public static DataService getInstance() {

Solution 1:

Data is loaded and synchronized from Firebase asynchronously.

It's easiest to see what's causing your problem if you add a few well-placed logging statements:

publicArrayList<UserDatabase> getFriendList() {

    mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
    final ArrayList<UserDatabase> list = newArrayList<>();

    System.out.println("Attaching listener");
    mDatabase.addChildEventListener(newChildEventListener() {
        @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
            System.out.println("Got data");
        }

        publicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {}
        publicvoidonChildRemoved(DataSnapshot dataSnapshot) {}
        publicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {}
        publicvoidonCancelled(DatabaseError databaseError) {}
    });

    System.out.println("Returning list");
    return list;
 }

The output will print in this order:

Attaching listener

Returning list

Got data

That is probably not the order you expected. But it does explain why the list of empty if you run the code as is.

The reason for this is that Firebase loads and synchronizes the data asynchronously. Like most modern web APIs it doesn't wait for the result to return (that would lead to a horrible user-experience), but instead lets the program continue and calls back into your code when it gets (new) data.

The way to deal with this behavior is to reframe the flow of your program from "first get the list of friends, then do xyz with it" to "whenever the list of friends changes, do xyz with it".

For example say that you simply want to print how many friends there are. Your current approach is probably:

ArrayList<UserDatabase> friends = getFriendList();
System.out.println("There are "+friends.size()+" friends");

With Firebase one way to handle this is to move the operation intogetFriendList():

publicvoidgetFriendList() {

    mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
    final ArrayList<UserDatabase> list = newArrayList<>();

    mDatabase.addChildEventListener(newChildEventListener() {
        @OverridepublicvoidonChildAdded(DataSnapshot dataSnapshot, String s) {
            UserDatabase userDatabase = dataSnapshot.getValue(UserDatabase.class);
            list.add(userDatabase);
            System.out.println("There are "+friends.size()+" friends");
        }

        publicvoidonChildChanged(DataSnapshot dataSnapshot, String s) {}
        publicvoidonChildRemoved(DataSnapshot dataSnapshot) {}
        publicvoidonChildMoved(DataSnapshot dataSnapshot, String s) {}
        publicvoidonCancelled(DatabaseError databaseError) {}
    });

 }

Post a Comment for "Arraylist Not Updating Inside Onchildadded Function"