0

For some reason when receiving data from fire base real time database, no matter which account I log into or whatever the database will always return "points" as 0, even when the value store in the database is not zero. The Uid is definitely correct as I checked that.I do not understand why and I have been trying to fix this problem for a while now.The code has worked in different activities and nothing has changed.

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) {

    }
});
Christian Webb
  • 297
  • 1
  • 6
  • 14
  • Could you edit your question to show what exactly is zero and how you know it's zero? Don't be afraid to put lots of logging statements in your code. – Doug Stevenson Mar 18 '18 at 20:50
  • "Points" is always returning as 0, even though it is not in the database.I have included the data in the database – Christian Webb Mar 18 '18 at 21:00
  • 1
    Please edit your question to show how you know the value is 0. Where are you checking its value in your code? What if you log its value immediately after you assign it in the callback? – Doug Stevenson Mar 18 '18 at 21:02
  • Do you get anything for totalpoints? – Levi Moreira Mar 18 '18 at 21:06
  • Total points returns as 0 aswell – Christian Webb Mar 18 '18 at 21:10
  • Add a log-statement or `throw databaseError.toException()` to the`onCancelled()` to see if they are getting called instead of `onDataChange()`. I suspect your security rules are preventing any value from being read and the zero values you are seeing are just the default initialization. – Bob Snyder Mar 18 '18 at 21:37

2 Answers2

1

To solve this, please use the following code:

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

Don't also forget to set your security rules like this:

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

So only authenticated users can read or write data to your database.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

The Solution that I found was to move the section of the code that displayed "points" in a text view to inside the "onDataChange" subroutine.

   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);
Christian Webb
  • 297
  • 1
  • 6
  • 14
  • This is what my answer does, right? So having that value, you can do whatever you want with it, obviously inside `onDataChange()` because this method has an [asynchronous behaviour](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774), correct? – Alex Mamo Mar 20 '18 at 07:21