首页 > 解决方案 > ValueEventListener 无休止地执行代码

问题描述

我的代码检查数据库中的值是否等于扫描条形码的值一切正常,但 Toast 或 SetValue 无休止地发生,直到 Qrcode 上的相机从 ValueEventListener 运行另一个活动我不能,所以我不知道如何执行此代码就一次。

这是发生此过程的代码

 // Called when a QR is decoded
// "text" : the text encoded in QR
  // "points" : points where QR control points are placed
  @Override
  public void onQRCodeRead(String text, PointF[] points) {
   pointsOverlayView.setPoints(points);

    DatabaseReference rootRef =          FirebaseDatabase.getInstance().getReference();
  DatabaseReference coinsUidRef =   rootRef.child("coinsUid").child(text);


  ValueEventListener valueEventListener = new ValueEventListener()    {
     @SuppressWarnings("ConstantConditions")
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    Log.v(text,""+dataSnapshot);
    if (!dataSnapshot.exists()) {
      Toast toast = Toast.makeText(getApplicationContext(),
              "Ошибка считвания! Считайте снова", Toast.LENGTH_SHORT);
      toast.show();


    } else {
      long qrCoinsAmount = dataSnapshot.getValue(Long.class);
      DatabaseReference coinsAmountRef = rootRef.child("users").child(getUid()).child("coinsAmount");
      ValueEventListener valueEventListener = new ValueEventListener() {


        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
          long coinsAmount = dataSnapshot.getValue(Long.class);
          coinsAmountRef.getRef().setValue(coinsAmount + qrCoinsAmount);
           coinsUidRef.removeValue();



        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
      };
      coinsAmountRef.addListenerForSingleValueEvent(valueEventListener);


    }


  }

  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {

  }

};
coinsUidRef.addListenerForSingleValueEvent(valueEventListener);

}

我的日志

   V   /http://erwe: DataSnapshot { key = erwe, value = null }
   V/http://erwe: DataSnapshot { key = erwe, value = null }
   I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
 V/http://erwe: DataSnapshot { key = erwe, value = null }
 V/http://erwe: DataSnapshot { key = erwe, value = null }
  I/Toast: Show toast from OpPackageName:com.example.ecohelp,     PackageName:com.example.ecohelp
 V/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
 I/Toast: Show toast from OpPackageName:com.example.ecohelp,   PackageName:com.example.ecohelp
  I/Toast: Show toast from OpPackageName:com.example.ecohelp,      PackageName:com.example.ecohelpV/http://erwe: DataSnapshot { key = erwe, value = null }
         V/http://erwe: DataSnapshot { key = erwe, value = null }
     I/Toast: Show toast from OpPackageName:com.example.ecohelp,   PackageName:com.example.ecohelp
    V/http://erwe: DataSnapshot { key = erwe, value = null }
       V/http://erwe: DataSnapshot { key = erwe, value = null }
      I/Toast: Show toast from OpPackageName:com.example.ecohelp,    PackageName:com.example.ecohelp
       V/http://erwe: DataSnapshot { key = erwe, value = null }
          V/http://erwe: DataSnapshot { key = erwe, value = null }
        I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
     I/Toast: Show toast from OpPackageName:com.example.ecohelp,      PackageName:com.example.ecohelp

PS我只是从数据库中删除了这个值,现在数据库中的值现在只更改一次(因为我需要)但是 Toast 仍然无休止地出现

标签: javaandroidfirebasefirebase-realtime-database

解决方案


尝试在 onDataChange 中删除 Value Listener

ref.removeEventListener(this);

因此,从 DataChange 回调中的两个引用中删除事件侦听器。看看这个答案

removeEventListener 未删除 firebase 中的侦听器


推荐阅读