Skip to content Skip to sidebar Skip to footer

Is Greenrobot Eventbus Sticky Events Order Guaranteed?

I'm starting using EventBus 3.0.0. I'm having 3 sticky events to be sent from Service to Activity: action started action progress action finished i'm listening to the events on m

Solution 1:

I haven't tried it but documentation here says you can indicate a priority for every onEvent method.

Guessing you have 3 event classes such as CompilationStart, CompilationProgress and CompilationFinish, you can provide a priority of 3, 2 and 1 respectively as bellow. Anyway, remember that default priority is 0, so any other event will be received before all of these ones:

@Subscribe(priority = 3);
publicvoidonEvent(CompilationStart event) {
    ...
}
@Subscribe(priority = 2);
publicvoidonEvent(CompilationProgress event) {
    ...
}
@Subscribe(priority = 1);
publicvoidonEvent(CompilationFinish event) {
    ...
}

On the other hand you can avoid using priority if you just use one class such as CompilationInfo being this as follows:

publicclassCompilationInfo {
    public CompilationState compilationState;
    publicint priority

    publicCompilationInfo(CompilationState compilationState, int priority){
        this.compilationState = compilationState;
        this.priority = priority;
    }
}

Where CompilationState is a parent class that CompilationStart, CompilationProgress and CompilationFinish hinerits from. It would be even better if you use an enum for priority field as it is more readable. This way you only need one onEvent method.

EDIT: Changed priority values according to documentation changes

Post a Comment for "Is Greenrobot Eventbus Sticky Events Order Guaranteed?"