0

Im using Firebase database to store my users scores games , at the end of the game users can see the final results of every members of the party

My users data and their scores are stored at 2 different places

here how looks my structure in Firebase.

Posts
-KLpcURDV68BcbAvlPFy
    user_id: "KLpcURDV68BcbAvlPFy"
    score: "A"
     
-asdasdasddsad
   user_id: "asdasdasddsad"
    score: "B"



Users
-KLpcURDV68BcbAvlPFy
    id: "KLpcURDV68BcbAvlPFy"
    name: "Jordan"
     
-asdasdasddsad
   id: "asdasdasddsad"
    name: "Tyler"

Here the code I use to retrieve users's data from Posts Child

private PostListResult GetInfoUsers( ) {
 DatabaseReference   mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    DatabaseReference   Posts = FirebaseDatabase.getInstance().getReference().child("Posts");

    PostListResult result = new PostListResult();

    List<User> list = new ArrayList<User>();


    Posts.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, Object> objectMap = (Map<String, Object>) dataSnapshot.getValue();

            if (objectMap != null) {

                for (String key : objectMap.keySet()) {
                    Object obj = objectMap.get(key);
                    if (obj instanceof Map) {
                        Map<String, Object> mapObj = (Map<String, Object>) obj;


                        String Userid = (String) mapObj.get("user_id");



                   


                        mDatabase.child(Userid).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                InfoUser User = dataSnapshot.getValue(InfoUser.class);
                                list.add(User);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                LogUtil.logError(TAG, "GetInfoUsers(), onCancelled", new Exception(databaseError.getMessage()));
                            }
                        });



                    }
                }




            }


            result.seUsers(list);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            LogUtil.logError(TAG, "getPostList(), onCancelled", new Exception(databaseError.getMessage()));
        }
    });




    return result;
}
 

I use the Id fund in the Posts child to retrieve all users info with for (String key : objectMap.keySet()) {} but my method always return empty , I thinks firebase has not enough time to collecte all the data before the (String key : objectMap.keySet()) {} return the result . Can you help me ?

}

  • You cannot simply return that value as a result of a method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a custom callback. – Alex Mamo Sep 19 '20 at 08:08
  • 1
    @AlexMamo thanks you for your help . –  Sep 19 '20 at 14:29

0 Answers0