简体   繁体   English

Firebase监听器在Java(Android)中被阻止

[英]Firebase listener is blocked in Java(Android)

I have a simple onChildAdded listener set, but it doesn't seem to be working: 我有一个简单的onChildAdded侦听器集,但它似乎不起作用:

listRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot,
            String previousChildName) {

        Log.d("DEBUG","A child has been added!");
        // Do stuff...

    }
    // Other unimplemented methods for ChildEventListener here...
});

while(true);

I'm adding children to listRef but nothing happens. 我正在将孩子添加到listRef中,但是什么也没有发生。

The while(true) line is here on purpose to demonstrate my problem, which is that the handler is only able to fire after everything else finishes! while(true)行是为了演示我的问题,这是处理程序仅在其他所有操作完成后才能够触发! And in this case, it never does. 在这种情况下,它永远不会这样做。 When I remove this line it works. 当我删除此行时,它起作用。

I assume that this is by design. 我认为这是设计使然。 Is there any way to make it work in these situations regardless of what comes after? 有什么办法可以使它们在这些情况下起作用,而不管发生了什么事?

Thanks 谢谢

while(true) would permanently block the thread from receiving events, since the Android client uses the main thread to post events. while(true)将永久阻止线程接收事件,因为Android客户端使用主线程发布事件。

It is possible to set a different thread to receive events via Config.setEventTarget() 可以设置其他线程以通过Config.setEventTarget()接收事件。

In this case, the EventTarget is passed instances of Runnable which it is responsible for running to receive events. 在这种情况下,EventTarget传递了Runnable的实例,该实例负责运行以接收事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM