我在使用 EventBus 3.0.0 时遇到问题,我在其中发布了一个这样的事件:
Call<List<SessionSpec>> call = httpService.getSessionSpecs();
call.enqueue(new Callback<List<SessionSpec>>() {
@Override
public void onResponse(Call<List<SessionSpec>> call, Response<List<SessionSpec>> response) {
if (response.isSuccessful()) {
List<SessionSpec> specs = response.body();
EventBus.getDefault().post((List<SessionSpec>)specs);
}
else Log.e(TAG, "sendSessionSpecs(): request NOT successful");
}
@Override
public void onFailure(Call<List<SessionSpec>> call, Throwable t) {
Log.e(TAG, "sendSessionsSpecs(): failed");
}
});
我在同一个片段中有两个订阅者,每个订阅者都有不同的签名,但他们都从一个帖子中被调用:
@Subscribe
public void onSessionSpec(List<SessionSpec> specs) {
Log.d(TAG, "onSessionSpec(): entered");
Log.d(TAG, " : number of session specs: " + specs.size());
}
第二个订阅者定义为:
@Subscribe
public void onOverlayType(List<OverlayType> types) {
Log.d(TAG, "onOverlayType(): entered");
Log.d(TAG, " : number of overlay types: " + types.size());
}
这两个回调都在一个片段中,该片段在帖子完成时处于活动状态,并且我已验证帖子仅被调用一次。当发布单个 SessionSpec 事件时,onSessionSpec 和 onOverlayType 回调都由 EventBus 调度,事件类型为 List>,因此 onOverlayType 回调在其回调参数中接收到错误的类型。OverlayType 类是一个简单的 POJO 类,它有 2 个成员,一个 int "sid" 和一个 String "name"。SessionSpec 类更复杂;它确实有一个成员字符串“名称”,但除此之外,这两个类之间没有其他共同点。我已经验证在 SessionSpec 类中没有与“OverlayType”非常相似的东西。
接口定义是这样的:
public interface VcapHttpInterface {
@GET("overlay/types")
Call<List<OverlayType>> getOverlayTypes();
@GET("session/list")
Call<List<SessionSpec>> getSessionSpecs();
@GET("session/{id}")
Call<Session> getSession(@Path("id") int sid);
}
getSession 事件发布/回调没有问题。
我整天都在试图弄清楚出了什么问题,所以我现在一无所知。有人知道我的代码可能有什么问题吗?
谢谢,-安德烈斯
编辑:EventBus 如何知道为特定响应调用哪个处理程序?我读过的一些帖子说 EventBus 不使用处理程序签名,但它怎么知道如何将响应映射到正确的订阅处理程序例程?有没有办法为给定事件显式定义处理程序回调?