1

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?

Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
  • Do we agree that you assume that within 1 minute you expect the method onDataChange to be called right? If so can you see it in your log file? – Nicolas Filotto May 06 '16 at 12:13
  • Yes I expect the method onDataChange to be called in within the timeframe of 1 minute. I can see the call in the logfile AFTER the minute has passed. After my edit I believe it has something to do with Threading, but this looks very weird to me. – Adrian Jandl May 06 '16 at 12:14
  • I believe that you should provide more code, to let us know how you call the method do() and how this even is triggered – Nicolas Filotto May 06 '16 at 13:33

0 Answers0