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
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.
DataSnapshotshould contain the data at the location, as it is known to the client where you have the listener. Since you're usingaddListenerForSingleValueEvent, I'm immediately suspicious of this coming from the local disk cache. so: are you using disk persistence? - Frank van Puffelen