0

I can't get to work this code to read (getUser method) from my Firebase DB. I have searched for answer for 2 hours, tried different tutorials and nothing helped. Problem is that it looks like the onDataChange method is never actually called (tested with Log) and I don't know what am I doing wrong. Writing to DB (saveUser method) is working as it should.

Code:

public class UserDatabase {

private User user;
private DatabaseReference databaseReference;

public UserDatabase() {
    databaseReference = FirebaseDatabase.getInstance().getReference();
}

public void saveUser(User user) {
    databaseReference.child("users").child(user.getUid()).setValue(user);
}

public User getUser(String uid) {
    databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(uid);
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return user;
} }

Thanks for answer

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jan Frait
  • 1
  • 2
  • 1
    Step 1, handle `onCancelled` as it *may* disclose problems: `public void onCancelled(DatabaseError databaseError) { throw databaseError.toException(); }` – Frank van Puffelen Feb 04 '18 at 17:58
  • Okay, added that line, sadly didn't solve the problem – Jan Frait Feb 04 '18 at 18:06
  • 1
    It isn't meant to solve the problem. But the app now crashes with a "permission denied", you at least know that the problem is caused by permissions. If the app now doesn't crash, you can rule out permissions and focus on the next step. The only reason I can think of that neither `onCancelled` nor `onDataChange` gets called, is if you're not connected to the internet (or at least Firebase's servers). – Frank van Puffelen Feb 04 '18 at 18:31
  • But my saveUser method is working in the same instance of app. So internet shouldn't be the problem. – Jan Frait Feb 04 '18 at 19:13
  • Okay, I have figured out the problem, it eventually gets the data, but before that it returns null user. I'll try to find the answer for that problem, but any advices are highly appreciated – Jan Frait Feb 04 '18 at 21:17

1 Answers1

0

As I see in your comment, you are saying that "eventually gets the data" but I tell you that the way in which you are trying to use the getUser() method which has as a return type the User class, is not the correct way to solve this problem, especially when it comes to asynchronous methods. You cannot return something now that hasn't been loaded yet. onDataChange() method has an asynchronous behaviour. A quick fix to your problem would be to use those user objects that you are getting from the database only inside onDataChange() method or, if you want to use those objects outside, please see the last part of my answer from this post, in which I explain the way in which you can use a callback in order to achieve this.

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