简体   繁体   English

使用Guava Eventbus时出现的线程问题和注意事项

[英]Thread issues and precautions when using guava eventbus

Can someone please mention - and explain if possible - the issues related to threads that I need to consider when using guava eventbus ? 有人可以提及(如果可能)并说明与使用番石榴事件总线时需要考虑的线程相关的问题吗? When I subscribe an android Activity and annotate one of its methods with (@Subscribe), and then post an event from another thread, I got an exception that the event didn't got dispatched. 当我订阅一个android Activity并使用(@Subscribe)注释其方法之一,然后从另一个线程发布事件时,我遇到了一个异常,即未调度该事件。

(I know I need to be on the UI thread to update the UI, that's not what I'm talking about. The event isn't event dispatched !) (我知道我需要在UI线程上更新UI,这不是我在说的。该事件不是事件调度的!)

EDIT: Here is an example : 编辑:这是一个示例:

post:(This runs in a networking thread) 帖子:(这在网络线程中运行)

eventBus.post(new EventShowToast("According to alarm \'" + alarm.getName() + "\', profile \'" + profile.getName() + "\' is run."));

subscribe:(a method in an activity, the activity register itself in onResume(...) ) 订阅:(活动中的方法,活动在onResume(...)中注册自己)

@Subscribe
    @AllowConcurrentEvents
    public void showToast(EventShowToast event) {
        showToast(event.getMsg());
    }

Are you registering the annotated class with the event bus. 您是否正在使用事件总线注册带注释的类。

eventBus.register(annotatedClassInstance);

Event Bus Explained 活动巴士说明

You can only toast from a UI thread. 您只能从UI线程敬酒。 EventBus catches all exceptions and then throws it's own "Could not dispatch event" exception, which is why it is hard to see what the true error was. EventBus捕获所有异常,然后引发它自己的“无法调度事件”异常,这就是为什么很难看到真正的错误是为什么的原因。

The solution to your problem can be found at Toast from a Non-UI thread 您可以通过非UI线程Toast上找到问题的解决方案

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

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