question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single....blockingGet() hangs in RxJava 2.1.3 when using Android/Firebase

See original GitHub issue

When using the following pattern to synchronously get data from Firebase Realtime Database:

String s = Single.create(new SingleOnSubscribe<String>() {
	@Override
	public void subscribe(SingleEmitter<String> e) throws Exception {
		FirebaseDatabase.getInstance().getReference("path").orderByChild("child")
                     .equalTo("xyz").addListenerForSingleValueEvent(new ValueEventListener() {
    		@Override
    		public void onDataChange(DataSnapshot dataSnapshot) {
        		e.onSuccess("Got it");
    		}
    		@Override
    		public void onCancelled(DatabaseError databaseError) {
        		e.onError(databaseError.toException());
    		}
		});
	}
}).blockingGet();

It will hang and create an ANR error. If I use the same Firebase “innards” outside of the Single, it fires just fine. The Single without the Firebase code inside also will fire, so it seems there is some incompatibility between the two.

[Apologies on the formatting, I can’t for the life of me get it to look nice]

Any ideas?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
akarnokdcommented, Aug 30, 2017

If blockingGet doesn’t return it means the ValueEventListener was not called. We had some issues when dealing with Android features that use weak references for their listeners: If you don’t have a strong reference to them, they may disappear and events are never signalled. Try this:

ValueEventListener vel = new ValueEventListener() {
    		@Override
    		public void onDataChange(DataSnapshot dataSnapshot) {
        		e.onSuccess("Got it");
    		}
    		@Override
    		public void onCancelled(DatabaseError databaseError) {
        		e.onError(databaseError.toException());
    		}
};

WhatEverThisTypeIs fb = FirebaseDatabase.getInstance()
                     .getReference("path")
                     .orderByChild("child")
                     .equalTo("xyz");
fb.addListenerForSingleValueEvent(vel);

e.setCancellation(() -> fb.removeListenerForSingleValueEvent(vel));

Also blocking on the main thread is discouraged so not sure on which you blocked originally and in your Rx. Assuming from the ANR that you block the main thread and the Firebase listener signals on the main thread but can’t since you are blocking it, hence a deadlock.

1reaction
akarnokdcommented, Aug 30, 2017

Please try replacing blockingGet with subscribe? If it works, then please consider my comment again:

you block the main thread and the Firebase listener signals on the main thread but can’t since you are blocking it, hence a deadlock.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Single.Create()...blockingGet() hangs in RxJava 2.1.3 when ...
Firebase delivers events on ui thread, waiting for result with blockingGet deadlocks it. In my opinion you should rethink app logic and ...
Read more >
signal | javascript reactive programming - kandi - Open Weaver
Implement signal with how-to, Q&A, fixes, code snippets. kandi ratings - Low support, No Bugs, No Vulnerabilities. Permissive License, Build not available.
Read more >
Single.Create()...blockingGet() hangs in RxJava 2.1.3 when using ...
When using the following pattern to anycodings_firebase synchronously get data from Firebase anycodings_firebase Realtime Database: String s = Single.create(new ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found