EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Property "priority" not respected

Open funnel20 opened this issue 3 years ago • 3 comments

We're struggling for days now to receive the events in the correct order. However, no matter what we do, events are always handled in the wrong order.

We use 2 different event classes:

public class ReadingsDataDidChangeNotification {
}
public class MeterReadingSavedNotification {
    public String token;
    public  MeterReadingSavedNotification(final String token) {
        this.token = token;
    }
}

These are posted from 2 different Classes in following order:

// Send Notification to refresh the RecyclerView:
Timber.d("Send ReadingsDataDidChangeNotification");
EventBus.getDefault().postSticky(new ReadingsDataDidChangeNotification());
// Send Notification to highlight the updated row in the RecyclerView for this meter reading:
Timber.d("Send MeterReadingSavedNotification");
EventBus.getDefault().postSticky(new MeterReadingSavedNotification(meter.token));

For the the receiving class MeterReadingsFragment it's important to always execute the action for ReadingsDataDidChangeNotification first, therefor it's set to a higher priority (2) than MeterReadingSavedNotification (1):

@Override
public void onStart() {
    super.onStart();
    Timber.d("Register EventBus");

    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    super.onStop();
    Timber.d("Unregister EventBus");

    EventBus.getDefault().unregister(this);
}

@Subscribe(priority = 2, sticky = true, threadMode = ThreadMode.MAIN)   // Highest priority to always be executed first
public void onMessageEvent(ReadingsDataDidChangeNotification event) {
    Timber.d("ReadingsDataDidChangeNotification");

    // Reload RecyclerView:
}

@Subscribe(priority = 1, sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(MeterReadingSavedNotification event) {
    Timber.d("MeterReadingSavedNotification");

    // Highlight row in RecyclerView and after a delay unhighlight:
}

However, when checking Logcat, the order is always the wrong one; MeterReadingSavedNotification is always called first, no matter what we do:

2020-11-06 11:38:16.059 15332-15332: Send ReadingsDataDidChangeNotification
2020-11-06 11:38:16.062 15332-15332: Send MeterReadingSavedNotification
2020-11-06 11:38:16.117 15332-15332: (MeterReadingsFragment.java:178)#onStart: Register EventBus
2020-11-06 11:38:16.125 15332-15332: (MeterReadingsFragment.java:156)#onResume: Resume
2020-11-06 11:38:16.178 15332-15332: (MeterReadingsFragment.java:324)#onMessageEvent: MeterReadingSavedNotification
2020-11-06 11:38:16.184 15332-15332: (MeterReadingsFragment.java:310)#onMessageEvent: ReadingsDataDidChangeNotification

We have:

  • used different ThreadMode options.
  • switched priority numbers.
  • changed the order of the @Subscribe() events in MeterReadingsFragment.

It seems that priority has no effect at all. Is this a bug, or do we implement it wrong?

funnel20 avatar Nov 06 '20 10:11 funnel20

Thanks. Did you read through https://greenrobot.org/eventbus/documentation/priorities-and-event-cancellation/?

greenrobot-team avatar Nov 24 '20 15:11 greenrobot-team

@greenrobot-team Yes of course.

funnel20 avatar Nov 24 '20 15:11 funnel20

same here. it seems like sticky events is not run with a priority but a LIFO strategy.

xxxifan avatar Feb 09 '21 08:02 xxxifan