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.

3
  • 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. Commented Mar 24, 2016 at 16:08
  • See my answer below. I have edited the question to remove it was a distraction. Commented Mar 24, 2016 at 17:21
  • I have accepted the answer as you were right after all - see my edit Commented Mar 25, 2016 at 17:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
Is your device connected to the internet? Check for airplane mode or monitor .info/connected
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...
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.
Good to hear! Now go and continue hacking with Firebase!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.