I have the following construct in code
public boolean do(){
final boolean[] returnValue = new boolean[1];
final CountDownLatch cdl = new CountDownLatch(1);
event.addListener(new Listener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange");
returnValue[0] = true;
cdl.countDown();
}
});
try {
if (cdl.await(1L, TimeUnit.MINUTES)) {
return returnValue[0];
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
What happens is the CountDownLatch
waits for 1 Minute and then the Listener triggers. But since the latch has already counted down false
is always returned. To me it seems as if the Latch is blocking the entire thread and not letting the Async Callback happen.
I have tried wrapping the event.addListener()...
part in a Runnable
but the Problem persists.
I have the exact same construct in another part of my code and there it works.
Edit:
I have put Logs for Thread.currentThread().getId()
and it does, in fact, evaluate to the same Id. Shouldn't the Async Callback be in a different Thread?