0

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?

Rana
  • 150
  • 1
  • 12
  • 1
    What do you mean by real time? Of course it'll take some time to fetch the data from any remote database. You can have a listener to the thread, so that when the data is fetch you'll have a function callback. – Reaz Murshed May 23 '16 at 18:54
  • am I right you using listener just to load data _once_? If yes, it's better to use some sync API. – Victor Sorokin May 23 '16 at 20:03
  • yeah, obviously, every call takes times, but I meant it's taking significant time even though I have a very small (10 key-value pairs) database. It's taking more than 7-8 seconds. Yes, I have a listener, but problem is that before getting an event, the main thread exits. So I wanted to know whether there's is a way to pause the main thread for the exact time, in which an event comes except sleep or sleep is the only way. If I'll use sleep here, then I have to put a hardcoded value which can be the larger than actual fetching time. – Rana May 23 '16 at 20:06
  • yes @Victor, I want to load data only once. You are right about sync API. I am also looking for sync Apis, but I am not aware of any in Firebase. Could you give some reference if you have?? – Rana May 23 '16 at 20:10
  • Sorry, have no exp with Firebase. This http://stackoverflow.com/a/11588115/162634 implies listeners API is first choice there. So, I can't see any real issue with your code. – Victor Sorokin May 23 '16 at 20:21

1 Answers1

0

You're returning UserDetail before the callback is finished. This is really about Async code organization rather than Firebase.

public void 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());
                    UserDetail userDetails = dataSnapshot.getValue(UserDetails.class);
                    //DO SOMETHING WITH USER DETAILS HERE!
                } else {
                    System.out.println("data snapshot is null!!");
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
        //DONT RETURN USER DETAILS HERE
    }

For more information take a look at AsyncTask or RxJava with Android

Max Alexander
  • 5,471
  • 6
  • 38
  • 52