I am trying to make a basic connection to firebase and get some data from firebase db, but before firing the callback, my main thread exits from the context and I didn't get any data in return. Below is my code:
public class UserStore {
private Firebase rootRef_;
private UserDetails userDetails;
public UserStore() {
rootRef_ = new Firebase("https://<FIREBASE-APP>.firebaseio.com/");
}
public UserDetails getUserDetails(String mailId) {
mailId = mailId.toLowerCase().replace(".", ",");
rootRef_.child("users/" + mailId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
System.out.println("user data snapshot: " + dataSnapshot.toString());
UserStore.this.userDetails = dataSnapshot.getValue(UserDetails.class);
} else {
System.out.println("data snapshot is null!!");
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return userDetails;
}
}
I am getting the correct response to the query, but it's taking time. I tried by using the sleep in this method. It worked fine but then it won't be real time.
Is there anyway, in which I can get the real time response?