public class DataService {
private static DataService ourInstance = new DataService();
private DatabaseReference mDatabase;
public static DataService getInstance() {
return ourInstance;
}
public ArrayList<UserDatabase> getFriendList() {
mDatabase = FirebaseDatabase.getInstance().getReference().child("users");
final ArrayList<UserDatabase> list = new ArrayList<>();
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserDatabase userDatabase = dataSnapshot.getValue(UserDatabase.class);
list.add(userDatabase);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
return list;
}
}
I am trying to fetch my users database from the users
node. It's acting really weird. When I set a breakpoint at the onChildAdded
function, it will retrieve all the users and add it to the list.
However, if I don't set a breakpoint there, it is not getting anything and the list size is 0. Does anyone have any idea what is going on? Thanks in advance!