chdryra
chdryra

Reputation: 523

Firebase on android not retrieving data

I have been able to successfully upload and delete data from my firebase repo using the guide here: https://www.firebase.com/docs/android/guide/saving-data.html

However if I try and retrieve the data using the the Firebase guide here: https://www.firebase.com/docs/android/guide/retrieving-data.html

the app activity just hangs - there is no call back to onDataChange or onCancel. I am using the exact code in the example which has worked until this point in the guide (in my Activity onCreate() method):

Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });

There is data at the example URL so should work. I am running on a phone running Android 4.0.3.

What am I doing wrong?

Thanks, Riz

Edit: I tried on my Samsung Note 4 running Android 5.1.1 and Firebase works fine. Doesn't work on my Huawei running Android 4.0.3. Something to do with the Android version?

Edit 2: So it turns out the answer below was right after all. I found an old bit of code in one of my helper classes that was pausing the thread whilst it waited for something. I rewrote it to be truly asynchronous and it fixed the problem. Many thanks - I have accepted the answer.

Upvotes: 0

Views: 2805

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Synchronizing data from Firebase is an asynchronous operation. Trying to wait for that data is not a good idea.

Your CallbackSignaler is blocking the onDataChange() from being called. If you remove the signaler, you'll see that the data is loaded, onDataChange() gets called and the value is logged.

See my answer to this question: Setting Singleton property value in Firebase Listener

Upvotes: 1

Related Questions