문제

I thought I had a nice solution to another question I asked, but now I don't think it will work out.

In Otto, if I do something like this:

@Subscribe public void foo(ApiRequestEvent<Foo> event) {
}

@Subscribe public void bar(ApiRequestEvent<Bar> even) {
}

It seems like both methods are called if I post any instance of ApiRequestEvent<> onto the bus. It that what is expected?

I am suspecting that this happens because in Java there is only really one class ApiRequestEvent, and Otto is looking at the class while ignoring the type parameters.

Before I abandon this approach, is there anyway around this? I was going to use this parameterized class as the "signature" for the events I wanted to subscribe to.

도움이 되었습니까?

해결책

That is correct. The type parameters will be ignored.

My typical approach is to define an Enum for all the event types, that the subscriber methods can check for to filter out events they are interested in.

Unfortunately since it can get cumbersome to enumerate all the event types, this of course doesn't scale very well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top