2

I want to retrieve a user from Firebase.

 public User findByUserName(String username){
    Query query = this.mDatabaseReference.orderByChild("username").equalTo(username);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            user = dataSnapshot.getValue(User.class);
        }

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

    return this.user;
}

The user is retrieved successfully only if there is a data changing event which makes perfect sense. However, I want to receive my user without changing anything. Is it possible?

If not, why would Firebase offer querying only in case of an event?

AL.
  • 36,815
  • 10
  • 142
  • 281
Teodor Dimitrov
  • 1,003
  • 2
  • 12
  • 19
  • 1
    All listeners fire once when first attached, a data change is not needed. If `onDataChange()` is not being called, there might be a problem with your security rules. Add a `Log` statement to `onCancelled()` to see if it is being called. – Bob Snyder Dec 07 '16 at 23:20
  • Thanks, the problem was that I was calling the method within a Facebook GraphRequest. It works exactly as you say. – Teodor Dimitrov Dec 08 '16 at 09:33

0 Answers0