java - ArrayList not updating inside onChildAdded function -
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 trying fetch users database users
node. it's acting weird. when set breakpoint @ onchildadded
function, retrieve users , add list.
however, if don't set breakpoint there, not getting , list size 0. have idea going on? in advance!
data loaded , synchronized firebase asynchronously.
it's easiest see what's causing problem if add few well-placed logging statements:
public arraylist<userdatabase> getfriendlist() { mdatabase = firebasedatabase.getinstance().getreference().child("users"); final arraylist<userdatabase> list = new arraylist<>(); system.out.println("attaching listener"); mdatabase.addchildeventlistener(new childeventlistener() { @override public void onchildadded(datasnapshot datasnapshot, string s) { system.out.println("got data"); } public void onchildchanged(datasnapshot datasnapshot, string s) {} public void onchildremoved(datasnapshot datasnapshot) {} public void onchildmoved(datasnapshot datasnapshot, string s) {} public void oncancelled(databaseerror databaseerror) {} }); system.out.println("returning list"); return list; }
the output print in order:
attaching listener
returning list
got data
that not order expected. explain why list of empty if run code is.
the reason firebase loads , synchronizes data asynchronously. modern web apis doesn't wait result return (that lead horrible user-experience), instead lets program continue , calls code when gets (new) data.
the way deal behavior reframe flow of program "first list of friends, xyz it" "whenever list of friends changes, xyz it".
for example want print how many friends there are. current approach probably:
arraylist<userdatabase> friends = getfriendlist(); system.out.println("there "+friends.size()+" friends");
with firebase 1 way handle move operation into getfriendlist()
:
public void 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); system.out.println("there "+friends.size()+" friends"); } public void onchildchanged(datasnapshot datasnapshot, string s) {} public void onchildremoved(datasnapshot datasnapshot) {} public void onchildmoved(datasnapshot datasnapshot, string s) {} public void oncancelled(databaseerror databaseerror) {} }); }
Comments
Post a Comment