removeEventListener not removing listener in firebase

前端 未结 4 1436
隐瞒了意图╮
隐瞒了意图╮ 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:30

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

提交回复
热议问题