使用guava eventbus时的线程问题及注意事项
Thread issues and precautions when using guava eventbus
有人可以提及 - 并在可能的情况下解释 - 我在使用 guava eventbus 时需要考虑的与线程相关的问题吗?
当我订阅一个 android Activity 并用 (@Subscribe) 注释它的一个方法,然后 post 来自另一个线程的事件时,我得到一个异常,该事件没有得到已发送。
(我知道我需要在 UI 线程上更新 UI,这不是我要说的。事件不是事件调度!)
编辑:
这是一个例子:
post:(这在网络线程中运行)
eventBus.post(new EventShowToast("According to alarm \'" + alarm.getName() + "\', profile \'" + profile.getName() + "\' is run."));
subscribe:(activity 中的方法,activity 在 onResume(...) 中注册自己)
@Subscribe
@AllowConcurrentEvents
public void showToast(EventShowToast event) {
showToast(event.getMsg());
}
您是否正在向事件总线注册带注释的 class。
eventBus.register(annotatedClassInstance);
您只能从 UI 话题中吐槽。 EventBus
捕获所有异常,然后抛出它自己的 "Could not dispatch event" 异常,这就是为什么很难看出真正的错误是什么。
您的问题的解决方案可以在 Toast from a Non-UI thread
找到
有人可以提及 - 并在可能的情况下解释 - 我在使用 guava eventbus 时需要考虑的与线程相关的问题吗? 当我订阅一个 android Activity 并用 (@Subscribe) 注释它的一个方法,然后 post 来自另一个线程的事件时,我得到一个异常,该事件没有得到已发送。
(我知道我需要在 UI 线程上更新 UI,这不是我要说的。事件不是事件调度!)
编辑: 这是一个例子:
post:(这在网络线程中运行)
eventBus.post(new EventShowToast("According to alarm \'" + alarm.getName() + "\', profile \'" + profile.getName() + "\' is run."));
subscribe:(activity 中的方法,activity 在 onResume(...) 中注册自己)
@Subscribe
@AllowConcurrentEvents
public void showToast(EventShowToast event) {
showToast(event.getMsg());
}
您是否正在向事件总线注册带注释的 class。
eventBus.register(annotatedClassInstance);
您只能从 UI 话题中吐槽。 EventBus
捕获所有异常,然后抛出它自己的 "Could not dispatch event" 异常,这就是为什么很难看出真正的错误是什么。
您的问题的解决方案可以在 Toast from a Non-UI thread
找到