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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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);
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 :/
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.
but the second listener, should i put it inside the onchange?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.