0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
chdryra
  • 523
  • 5
  • 17
  • There is a **very** good chance that the `CallBackSignaler` is simply blocking the main thread and hanging your app. See [my answer to this question](http://stackoverflow.com/a/33204705/209103). – Frank van Puffelen Mar 24 '16 at 16:08
  • See my answer below. I have edited the question to remove it was a distraction. – chdryra Mar 24 '16 at 17:21
  • I have accepted the answer as you were right after all - see my edit – chdryra Mar 25 '16 at 17:44

1 Answers1

1

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

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks. It's not the signaler as I only put that in once I realised it was hanging to check for the callback without having to resort to the debugger. All my classes dealing with persistence are asynchronous by design - the callback signaler is just a debugging tool for problems like this. – chdryra Mar 24 '16 at 17:14
  • Is your device connected to the internet? Check for airplane mode or [monitor `.info/connected`](https://www.firebase.com/docs/android/guide/offline-capabilities.html#section-connection-state) – Frank van Puffelen Mar 24 '16 at 18:30
  • I have created a new clean project in Android studio with only the Firebase example inside the MainActivity. It seems to work on both phones fine now. I am not sure what is going on but there's a bunch of asynchronous stuff going on in my main app when I first fire it up as data is loaded. I'm wondering if there is some threading issue related to the answer you gave initially after all... – chdryra Mar 24 '16 at 18:52
  • So it turns out you were 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. – chdryra Mar 25 '16 at 17:43
  • Good to hear! Now go and continue hacking with Firebase! – Frank van Puffelen Mar 25 '16 at 19:09