Firebase in Data Change is called multiple times in add event event

I have set up two listeners

  • Request to get the last message
  • If the last message is a group type, I get the group information using the group Listener

However, when I get the message type using messageListener and set the groupListener, it returns multiple onDataChange Debug.e("parentsnap",dataSnapshot.getValue().toString());

called more than once How do I do this please send

    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);

      

+3


source to share


2 answers


If you need to receive data once, you need to use addListenerForSingleValueEvent(...)

instead addValueEventListener(...)

. Then onDataChange()

will only come back once



+2


source


Check if you have

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

      



and now test without it.

If you are storing data locally and using it addListenerForSingleValueEvent(...)

, this can result in multiple (2) calls to get fresh data.

+1


source







All Articles