Search code examples
androidotto

Otto events handling order


Please help me to figure out if the following scenarios are valid if I'm going to use Otto Bus lib.

  1. If I need to ensure the order of subscriber execution. I have two component that are listening for the same event type,is there the Otto Bus capability which can guarantee that constantly component1 handles an event before component2. Could the order I register them provide me that?
  2. According to the Otto documentation

    Posting to the bus is a synchronous action so when program execution continues it is guaranteed that all subscribers have been called.

    Does it mean that any of event subscribers are not executed in parallel?

Thanks


Solution

  • When posting an event, Otto iterates through a Set of handlers to dispatch the event to them.

    Since Set is an unordered collection, it is not guaranteed that this will happen in the order you've registered them (or any particular order).

    The relevant part from the source code:

    Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());
    
    boolean dispatched = false;
    for (Class<?> eventType : dispatchTypes) {
      Set<EventHandler> wrappers = getHandlersForEventType(eventType);
    
      if (wrappers != null && !wrappers.isEmpty()) {
        dispatched = true;
        for (EventHandler wrapper : wrappers) {
          enqueueEvent(event, wrapper);
        }
      }
    }