Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep track of listeners in Firebase on Android?

I have a list of chat rooms for a given user at one location, and total number of messages for a given chat room at another location. I want to keep track of a number of messages at chatrooms user belongs to.

I have a following snippet:

//getting all chat rooms this user belongs to
mFirebase.child("myChatRooms").child("testUser").addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded (DataSnapshot dataSnapshot, String previousKey){
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
            ChatRoom myRoom = postSnapshot.getValue(ChatRoom.class);

            //listening to  message count in every chat room user belongs to
            mFirebase.child("chatRoomMessageCount").child(postSnapshot.getKey()).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                        //number of messages have changed
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }
    }

    //......
}

The question is, how do I remove all those ValueEventListener's later, when onChildRemoved will be called, or I won't need them anymore?

What is the recommended approach in dealing with this situation? Should I store child key and listener in HashMap and keep track of them myself or there is some way to remove all listeners for a given firebase location?

like image 733
Sver Avatar asked Nov 18 '15 09:11

Sver


1 Answers

A data conscientious way would be to create a HashMap of ValueEventListeners and then in onStop or onDestroy you would remove those listeners.

To keep things simple, you should use a single event listener to get the rooms. Then for each room you can create a realtime listener.

public class MainActivity extends Activity {

    private Firebase mRef;
    private HashMap<Firebase, ValueEventListener> mListenerMap;

    @Override
    protected void onStart() {
        super.onStart();

        mRef = new Firebase("https://<your-firebase-app>/myChatRooms/testUser");

        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                    ValueEventListener listener = new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            // do your thing here
                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {

                        }
                    };
                    mListenerMap.put(postSnapshot.getRef(), listener);
                    Firebase childRef = mRef.child("chatRoomMessageCount").child(postSnapshot.getKey());
                    childRef.addValueEventListener(listener);
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        for (Map.Entry<Firebase, ValueEventListener> entry : mListenerMap.entrySet()) {
            Firebase ref = entry.getKey();
            ValueEventListener listener = entry.getValue();
            ref.removeEventListener(listener);
        }
    }
}
like image 92
David East Avatar answered Nov 15 '22 13:11

David East