EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

NoSubscriberEvent is not called after unregister

Open hohnamkung opened this issue 13 years ago • 1 comments

I make a example testcast using EventBusNoSubscriberEventTest.java (in EventBusTest)

public class EventBusNoSubscriberEventTest2 extends AbstractEventBusTest {
    public void testNoSubscriberEvent() {

        eventBus.register(this, NoSubscriberEvent.class);
        eventBus.register(this, String.class);
        eventBus.post("Foo");
        String event = (String) lastEvent;
        assertEquals("Foo", event);
        eventBus.unregister(this, String.class);

        eventBus.post("Bar");
        assertEquals(NoSubscriberEvent.class, lastEvent.getClass());
        NoSubscriberEvent noSub = (NoSubscriberEvent) lastEvent;
        assertEquals("Bar", noSub.originalEvent);
    }

    public void onEvent(NoSubscriberEvent event) {
        trackEvent(event);
    }

    public void onEvent(String message) {
        trackEvent(message);
    }
}

This testcast fail. (lastEvent type is not NoSubscriberEvent)

expected:<class de.greenrobot.event.NoSubscriberEvent> but was:<class java.lang.String>

The cause of this problem is here

private void postSingleEvent(Object event, boolean isMainThread) throws Error {
    ...
        if (subscriptions != null) {
            for (Subscription subscription : subscriptions) {
                postToSubscription(subscription, event, isMainThread);
            }
            subscriptionFound = true;
        }
    ...
}

After unregister subscriptions can be a empty list (not null)

hohnamkung avatar Jul 11 '13 13:07 hohnamkung

Thanks for reporting and investigating this issue! 86d14d2313e67d2225875fbac93761f7403fe319 will be released with 2.1.1 in the near future.

greenrobot avatar Nov 15 '13 13:11 greenrobot