removeEventListener not removing listener in firebase

前端 未结 4 1411
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 10:40

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

ValueEventListener valueListener=null;

private void          


        
4条回答
  •  误落风尘
    2021-02-19 11:27

    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.

提交回复
热议问题