Exemplo n.º 1
0
  /**
   * Posts the given event to the event bus. post 函数会首先得到当前线程的 post
   * 信息PostingThreadState,其中包含事件队列,将当前事件添加到其事件队列中,然后循环调用 postSingleEvent 函数发布队列中的每个事件。
   */
  public void post(Object event) {
    PostingThreadState postingState =
        currentPostingThreadState.get(); // 得到当前线程的 post 信息PostingThreadState
    List<Object> eventQueue = postingState.eventQueue;
    eventQueue.add(event); // 将当前事件添加到其事件队列中

    if (!postingState.isPosting) {
      postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
      postingState.isPosting = true;
      if (postingState.canceled) {
        throw new EventBusException("Internal error. Abort state was not reset");
      }
      try {
        while (!eventQueue.isEmpty()) { // 循环调用 postSingleEvent 函数发布队列中的每个事件
          postSingleEvent(eventQueue.remove(0), postingState);
        }
      } finally {
        postingState.isPosting = false;
        postingState.isMainThread = false;
      }
    }
  }