Skip to content

fix the issue of Property priority not respected in sticky event #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions EventBus/src/org/greenrobot/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.greenrobot.eventbus.android.AndroidDependenciesDetector;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -148,6 +149,7 @@ public void register(Object subscriber) {

Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
Collections.sort(subscriberMethods);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
Expand Down
7 changes: 6 additions & 1 deletion EventBus/src/org/greenrobot/eventbus/SubscriberMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.lang.reflect.Method;

/** Used internally by EventBus and generated subscriber indexes. */
public class SubscriberMethod {
public class SubscriberMethod implements Comparable<SubscriberMethod>{
final Method method;
final ThreadMode threadMode;
final Class<?> eventType;
Expand Down Expand Up @@ -65,4 +65,9 @@ private synchronized void checkMethodString() {
public int hashCode() {
return method.hashCode();
}

@Override
public int compareTo(SubscriberMethod subscriberMethod) {
return Integer.compare(subscriberMethod.priority,this.priority);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
public class EventBusStickyEventTest extends AbstractEventBusTest {

private int a = 0;

@Test
public void testPostSticky() throws InterruptedException {
eventBus.postSticky("Sticky");
Expand Down Expand Up @@ -56,6 +58,14 @@ public void testPostStickyTwoSubscribers() throws InterruptedException {
assertEquals(6, eventCount.intValue());
}

@Test
public void testPostTwoDiffPriorityStickyEventOrder() throws InterruptedException {
eventBus.postSticky("Sticky");
eventBus.postSticky(1);
eventBus.register(new TwoDiffPriorityStickyEventSubscriber());
assertEquals(1, a);
}

@Test
public void testPostStickyRegisterNonSticky() throws InterruptedException {
eventBus.postSticky("Sticky");
Expand Down Expand Up @@ -187,6 +197,19 @@ public void onEvent(IntTestEvent event) {
}
}

public class TwoDiffPriorityStickyEventSubscriber {

@Subscribe(sticky = true,priority = 1)
public void onEvent(String event) {
a = 1;
}

@Subscribe(sticky = true,priority = 2)
public void onEvent(Integer event) {
a = 2;
}
}

public class StickyIntTestSubscriber {
@Subscribe(sticky = true)
public void onEvent(IntTestEvent event) {
Expand Down