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.
You can remove the listener from within the callback with:
So a complete fragment:
I removed the
HashMap
, instead using the methods of theDataSnapshot
to accomplish the same. I also renamed a few variables to be clearer/more idiomatic.