1

I am developing a chat app but when it comes to viewing friend requests to the user i face a problem that is

When I log queries of Firebase why is it not properly ordered as code ?

This is my code:

 // Query for pending requests in the current user
    Log.v("AAAAAA", "AAAAAA");
    usersRef.child(MainActivity.currentUser.getUid()).child("inpendingfriendreq").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) { // dataSnapShot = inpendingfriendreq node
            for (DataSnapshot reqDataSnapShot : dataSnapshot.getChildren()) {
                U = reqDataSnapShot;
                Log.v("BBBBBBBB", U.toString());
                usersRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot sDataSnapshot) { // sDataSnapshot = "users" node DataSnapShot
                        for (DataSnapshot searchForPendingUserDS : sDataSnapshot.getChildren()) {  // searchForPendingUserDS = a user from database (child node of "users" node)
                            Log.v("CCCCCCC", searchForPendingUserDS.toString());

                            if (searchForPendingUserDS.getKey().equals(U.getKey())) { // If that user's id equals pending req id
                                MainActivity.reqArrList.add(new FriendRequest(R.drawable.default_pp, searchForPendingUserDS.child("name").getValue(String.class)));
                                Log.v("DDDDDDDDD", searchForPendingUserDS.child("name").getValue(String.class));

                            }

                        }
                        Log.v("EEEEEEEE", MainActivity.reqArrList.toString());
                        //requestsPageUpdated();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(mContext, "Error querying", Toast.LENGTH_SHORT).show();
                    }
                });
                //requestsPageUpdated();
                Log.v("FFFFFFFFFF", "FFFFFFFFF");
                break;
            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(mContext, "Error querying", Toast.LENGTH_SHORT).show();
        }

    });

It should be:

AAAAAA
    BBBBBB
        CCCCCC
            DDDDDD
        CCCCCC
        CCCCCC
        CCCCCC
        EEEEEE
    FFFFFF
GGGGGG

But it shows that in the Logcat:

AAAAAA
GGGGGG
BBBBBB
FFFFFF
CCCCCC
DDDDDD
CCCCCC
CCCCCC
CCCCCC
EEEEEE

I know that event listeners are Async but what is the meaning of that ? Does Async means that the function are waiting to be triggered like Button onClickListener or does it mean that when it's triggered it runs parallel to the main thread? so in the second case that will make sense to my problem. Any help, please!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Amr Salah
  • 85
  • 9
  • Async means it runs parallel. Check this [how async work](https://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean) – sanoJ Jul 20 '19 at 05:53
  • Thanks! So, my question now is how can i arrange this order to the correct order that the above code should be sync ? – Amr Salah Jul 20 '19 at 06:06
  • Check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Jul 20 '19 at 08:12

0 Answers0