0
votes

I've just started trying to use Firebase in my Android application. I can write data into the database fine but I'm running into a problem when trying to retrieve data.

My database is structured as below
enter image description here


My method for retrieving the data:

public void getCurrentUserData() {

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        FirebaseUser loggedUser = firebaseAuth.getCurrentUser();
        String uid = loggedUser.getUid();

        DatabaseReference userRef = database.getReference().child("Users").child(uid);

        userRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //crashes because user is not a string
                //User user = dataSnapshot.getValue(User.class);

                //works because function is returning first child of uID as a string
                String user = dataSnapshot.getValue().toString();
            }

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

            }
        });

    }


When debugging:

dataSnapshot = "DataSnapshot { key = bio, value = test }"


I was expecting this to return all children contained within uid (bio, dob, firstName, joinDate, lastName, location) and put them into my User object but it actually looks as if its only returning the first child node (bio) as a string.

Why is this happening and how do I retrieve the full set of child nodes?

Any help appreciated. Thanks.

1
The DataSnapshot should contain the data at the location, as it is known to the client where you have the listener. Since you're using addListenerForSingleValueEvent, I'm immediately suspicious of this coming from the local disk cache. so: are you using disk persistence? - Frank van Puffelen
Not that I'm aware of? I've change the data contained within 'bio' and re run the application, it always updates. - Paul Alexander
Can you post your User class? Could be something in it that is not compatible with Firebase. Are you missing an empty, default constructor in your User class? this was an issue for me once. - David Velasquez
@FrankvanPuffelen The poster says he was trying to "put them into my User object". I think he commented out that line to try the string version that does not crash. But to me it seems his end goal is for Firebase to return the User object. - David Velasquez
Guys, I've looked over my User class... I was missing a setter for one of my values :/ thanks for your help - Paul Alexander

1 Answers

1
votes

If you want to get all properties of the user, you will either need to create a custom Java class to represent that user, or you will need to read the properties from the individual child snapshots in your own code. For example:

userRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String firstName = dataSnapshot.child("firstName").getValue(String.class);
        String lastName = dataSnapshot.child("lastName").getValue(String.class);
        ...
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Alternatively you can loop over the properties with:

userRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot propertySnapshot: dataSnapshot.getChildren()) {
            System.out.println(propertySnapshot.getKey()+": "+propertySnapshot.getValue(String.class));
        }
    }