removeEventListener not removing listener in firebase

25.6k views Asked by At

I want to remove addValueEventListener listener from a firebase ref when value of particular field is true.

ValueEventListener valueListener=null;

private void removeListener(Firebase fb){
    if(valueListener!=null){
        **fb.removeEventListener(valueListener);**
    }
}

String key="https://boiling-heat-3083.firebaseio.com/baseNodeAttempt/" + userId+"/"+nodeType+"/"+nodeId+"/data";
final Firebase fb = new Firebase(key);
valueListener=fb.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snap) {
        final HashMap<String, Object> data=(HashMap<String, Object>) snap.getValue();
        if( data.get("attemptFinish_"+nodeId)!=null){
            boolean title = (boolean) snap.child("attemptFinish_"+nodeId).getValue();
            if(title){
                removeListener(fb);
            }
        }
    }
    @Override
    public void onCancelled() {
        // TODO Auto-generated method stub
    }
});

But addValueEventListener is not getting removed and it's called for that firebase ref . So please suggest me how to remove listener from any firebase ref if required.

4

There are 4 answers

5
Frank van Puffelen On

You can remove the listener from within the callback with:

ref.removeEventListener(this);

So a complete fragment:

String key="https://boiling-heat-3083.firebaseio.com/baseNodeAttempt/" + userId+"/"+nodeType+"/"+nodeId+"/data";
final Firebase ref = new Firebase(key);
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snap) {
        if (snap.hasChild("attemptFinish_"+nodeId) {
            boolean isFinished = (boolean) snap.child("attemptFinish_"+nodeId).getValue();
            if(isFinished){
                ref.removeEventListener(this);
            }
        }
    }
    @Override
    public void onCancelled() {
        // TODO Auto-generated method stub
    }
});

I removed the HashMap, instead using the methods of the DataSnapshot to accomplish the same. I also renamed a few variables to be clearer/more idiomatic.

0
Coldfin Lab On
private DatabaseReference dbRef;
ValueEventListener mSendEventListner;

dbRef = FirebaseDatabase.getInstance().getReference().child("users").child(phone);

ValueEventListener valueEventListener = new ValueEventListener() 
{
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists())
                {              
                   //
                } 
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                //
            }
};
dbRef.addValueEventListener(valueEventListener);
mSendEventListner = valueEventListener;


//REMOVE VALUE EVENTLISTNER...
@Override
protected void onDestroy() 
{
    super.onDestroy();
    if (mSendEventListner != null) {
        dbRef.removeEventListener(mSendEventListner);
    }
}
0
Mathan Chinna On

I hope this will help you... just yo can use this method, it's work for me...

private DatabaseReference dbRef;

Write from onCreate()

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    dbRef= database.getReference("YOUR_PATH").child("KEY_NAME");
    InitListener();
    dbRef.addValueEventListener(valueEventListener); // ADDIND_LISTENER

Write from onDestroy()

if (dbRef!=null){
        dbRef.removeEventListener(valueEventListener);
        valueEventListener=null;
        dbRef=null;

    }

Listener

 private void InitListener(String templeId) {
    valueEventListener=new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

}

1
Andre Haueisen On

Make sure you add and remove the listener to the same node on your DatabaseReference. For example:

//when declared like this, mDatabaseReference will point to the parent node by default
private DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//listener added to child node "path_2"
mDatabaseReference.child(path_1).child(path_2).addChildEventListener(myListener); 
}

Your listener is this case is pointing to path_2. If you try to remove your listener using this code

//mDatabaseReference pointing to parent node (default behaviour)
mDatabaseReference.removeEventListener(myListener);

it won't work because you are trying to remove the listener from the wrong node. The correct way would be

mDatabaseReference.child(path_1).child(path_2).removeEventListener(myListener);