Mostafa Khaled
Mostafa Khaled

Reputation: 372

addListenerForSingleValueEvent is fired multiple times

I am using firebase database in my app. I am using addListenerForSingleValueEvent Listner to update data in my data base. I found that the data isn't updating correctly and after debugging the code I found that the listener has worked for 5 times !!!

            if (player1Score == player2Score) {
            player1Reference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.v("pointsDebug", "onDataChanged1");
                    long points = (long) dataSnapshot.child("points").getValue();
                    usersReference.child(player1Uid).child("points").setValue(points + 1);

                    usersReference.removeEventListener(this);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            player2Reference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.v("pointsDebug", "onDataChanged2");
                    long points = (long) dataSnapshot.child("points").getValue();
                    usersReference.child(player2Uid).child("points").setValue(points + 1);

                    usersReference.removeEventListener(this);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

and this is the repeated log :

10-20 17:03:43.841 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged1
10-20 17:03:43.841 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged2
10-20 17:03:43.881 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged1
10-20 17:03:43.881 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged2
10-20 17:03:43.921 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged1
10-20 17:03:43.921 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged2
10-20 17:03:43.971 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged1
10-20 17:03:43.971 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged2
10-20 17:03:44.691 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged1
10-20 17:03:44.701 19217-19217/com.mk.playAndLearn V/pointsDebug: onDataChanged2

The database structre is something like that :

users
   -user1
        -....
   -user2
        -.....

Upvotes: 0

Views: 384

Answers (1)

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

The best and the ideal approach to remove the listener is to do it based on the Android Life-cycle of the current Activity.

But, you are removing the listener inside the onDataChange(). I guess that's why this happens.

Either remove this line: usersReference.removeEventListener(this); from onDataChange() or, remove the listener inside onDestory() method:

@Override
protected void onDestroy() {
    super.onDestroy();
    usersReference.removeEventListener(YourActivity.this);
}

Android will do the rest.

Upvotes: 1

Related Questions