如何阻止Firebase数据库始终返回0?

时间:2018-03-18 20:47:35

标签: android firebase firebase-realtime-database firebase-authentication

出于某种原因,当从火灾基地实时数据库接收数据时,无论我登录哪个帐户,或者数据库将始终返回什么,"积分"即使数据库中的值存储不为零,也为0。 Uid肯定是正确的,因为我检查了。我不明白为什么,我一直试图解决这个问题一段时间。代码已经在不同的活动中工作,没有任何改变。

This is the Data that is store in the fire base database

    databaseUsers =FirebaseDatabase.getInstance().getReference("Users");
    final FirebaseAuth firebaseAuth;
    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getCurrentUser();
    databaseUsers.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        points = dataSnapshot.child(user.getUid()).child("points").getValue(int.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
//Getting Total Points from Firebase Database
databaseUsers.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        totalpoints = dataSnapshot.child(user.getUid()).child("totalpoints").getValue(int.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

2 个答案:

答案 0 :(得分:1)

要解决此问题,请使用以下代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long points = dataSnapshot.child("points").getValue(Long.class);
        Log.d("TAG", points + "");
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

也不要忘记设置这样的安全规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

因此,只有经过身份验证的用户才能读取或写入数据库数据。

答案 1 :(得分:0)

我找到的解决方案是移动显示的代码部分" points"在文本视图中的" onDataChange"子程序。

   DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference uidRef = rootRef.child("Users").child(uid);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Integer value = dataSnapshot.child("points").getValue(Integer.class);
            if (value != null) {
                points = value;
            } else {
                points = 0;
            }
            value = dataSnapshot.child("totalpoints").getValue(Integer.class);
            if (value != null) {
                totalpoints = value;
            } else {
                totalpoints = 0;
            }

            txtpoints.setText("Points: "+ points);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d("my tag","data Snapshot Cancelled");
        }
    };
    uidRef.addListenerForSingleValueEvent(valueEventListener);