0

I'm working with firebase in a android application and something strange ocurred, in my example I need two snapshots, 1 to get the users inside a list ( I use this snapshot to fill a arraylist of strings with the key of the user) and the other to compare to the users, the strange behavior is that my arraylist is empty after the first snapshot, I used logcat to check it and that Log inside the firstsnapshot returns me 1 as the size of the arraylist, the second returns me 0, dunno how it gets 0 again.

Here is my code:

 private void prepareFriendList() {
        myRef.child(id).child("FriendLists").child(group).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String keyUser;
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Log.d("number:",snapshot.getKey());
                    keyUser = snapshot.getKey();
                    currentFriends.add(keyUser);
                    Log.d("hello",String.valueOf(currentFriends.size()));

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        for(String s: currentFriends){
            Log.d("idddd",s);
        }

        Log.d("hello",String.valueOf(currentFriends.size()));



        myRef.child(id).child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    User user = snapshot.getValue(User.class);

                    for(String item: currentFriends){
                        if (snapshot.getKey().equals(item)) {
                            usersList.add(user);
                        }
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mAdapter.notifyDataSetChanged();
    }

I don't understand why that happens, since I'm adding the key inside the arraylist, any tip?

KENdi
  • 7,576
  • 2
  • 16
  • 31

1 Answers1

1

This is happening because onDataChange is called asynchronously. This means that the statement that adds users to the list is executed before onDataChange has been called. That's why your list is empty outside that method. So in order to use that lists, you need to use it inside the onDataChange() method.

For other approach, please visit this post and this post.

Hope it helps.

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Well i understood the problem thank you for your response, but the thing is i can't implement a listener inside a listener, because i need 2 diferent references i tried to put my second listener inside the onchange, but it never enters there :S any suggestion? – Andre Silva Apr 25 '17 at 14:14
  • Yes you can use nested `listeners`. As long as each listener has a specific `reference`, there will be no problem. In the end, don't forget to remove the `listeners` in your `onDestroy` method like this: `yourReference.removeEventListener(valueEventListener);` – Alex Mamo Apr 25 '17 at 14:21
  • can you exemplify how can i put my second 'myRef' code inside the first 'myRef' code, because i tried it and it doesn't work :/ – Andre Silva Apr 25 '17 at 14:25
  • The principle is very simple. Declare the `list` inside fisrt `listener`. Move the second listener inside the first and than loop trought the list in the end. Remember, you need to declare the list inside the first and to loop inside the second. – Alex Mamo Apr 25 '17 at 14:31
  • but the second listener, should i put it inside the onchange? – Andre Silva Apr 25 '17 at 14:34
  • Glad to help. Cheers! – Alex Mamo Apr 25 '17 at 18:21