Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase on Data Change is called multiple times in add value event listener

I have set two listeners

  1. Query to get the last message
  2. If the last message is group type I'm getting group info using group Listener

However, when I get the type from the message using messageListener and set groupListener it returns multiple onDataChange Debug.e("parentsnap",dataSnapshot.getValue().toString()); gets called more than one times How should I do it, please guide

    groupListener = new ValueEventListener() {
        @SuppressWarnings("unchecked")
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Debug.e("parent snap", dataSnapshot.getValue().toString());
            for (DataSnapshot d :
                    dataSnapshot.getChildren()) {
           //my code
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    groups.child(chatId).addValueEventListener(groupListener);
like image 264
rookieDeveloper Avatar asked May 18 '17 05:05

rookieDeveloper


1 Answers

If you need to receive data one time you need to use addListenerForSingleValueEvent(...) instead of addValueEventListener(...). Then onDataChange() will return only one time

like image 191
Linh Avatar answered Oct 21 '22 06:10

Linh