Android firebase value event listener nested inside another listener fetching data in an abnormal way -


this first question in stackoverflow. use firebase in android app, , db schema follows:

users schema:

users schema

and

posts schema:

posts schema

now, have used recyclerview show posts users. have implemented singlevalueeventlistener list of posts users , eventlistener nested inside fetch user's name , profile picture.

the code follows:

query query = databasereference.child("posts");         query.addlistenerforsinglevalueevent(new valueeventlistener() {             @override             public void ondatachange(datasnapshot datasnapshot) {                 for(datasnapshot data : datasnapshot.getchildren())                 {                     final postspojo postspojo = data.getvalue(postspojo.class);                     images = postspojo.getcontent_post();                      //nested listener fetch user's name , profile picture node "users/userid"                     final query userdetails = databasereference.child("users/"+postspojo.getuserid());                     mlistener = new valueeventlistener() {                          @override                         public void ondatachange(datasnapshot datasnapshot1) {                              string username = datasnapshot1.child("username").getvalue(string.class);                             string profilepicturepath = datasnapshot1.child("profilepicture").getvalue(string.class);                              list.add(new postspojo(postspojo.getuserid(),profilepicturepath ,username, postspojo.gettimestamp(),postspojo.getposttext(),postspojo.getlocation(),postspojo.getcontent_post()));                             log.d("datalist", postspojo.getuserid()+","+profilepicturepath +","+username+","+postspojo.gettimestamp()+","+postspojo.getposttext()+","+postspojo.getlocation()+","+postspojo.getcontent_post());                              newsfeedlistadapter.notifydatasetchanged();                         }                         @override                         public void oncancelled(databaseerror databaseerror) {                         }                     };                      userdetails.addlistenerforsinglevalueevent(mlistener);                 }             }             @override             public void oncancelled(databaseerror databaseerror) {             }         }); 

now, problem first userid nested listener gets, fetches posts particular userid first , goes on next userid example if gets userid : 1, give posts user first , go on next one, if userid : 2, vice versa. want posts data userid associated post.

i have implemented .orderbykey() no success.

thank you.

you can put post ids under user data like

users -> 1 -> posts -> [postid-1,postid-2,postid-3]


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -