2016-02-09 60 views
1

我在我的android应用程序中使用Otto作为我的事件总线。
我必须确保某些事件被称为主线程,或者没有在主线程,为我创造它采用奥托一样,所以我自己的总线类:Otto的每种方法的线程强制执行

class MyEventBus { 
    private final Bus anyThreadBus; 
    private final Bus mainThreadBus; 
    private final Bus notMainThreadBus; 

    private final Handler mainThreadHandler; 

    public enum Strategy { 
     Any, 
     Main, 
     NotMain 
    } 

    MyEventBus() { 
     this.anyThreadBus = new Bus(ThreadEnforcer.ANY); 
     this.mainThreadBus = new Bus(ThreadEnforcer.MAIN); 
     this.notMainThreadBus = new Bus(ThreadEnforcer.ANY); 
     this.mainThreadHandler = new Handler(Looper.getMainLooper()); 
    } 

    public void register(Object object) { 
     this.register(object, Strategy.Any); 
    } 

    public void register(Object object, Strategy strategy) { 
     switch (strategy) { 
      case Main: 
       this.mainThreadBus.register(object); 
       break; 

      case NotMain: 
       this.notMainThreadBus.register(object); 
       break; 

      case Any: 
      default: 
       this.anyThreadBus.register(object); 
     } 
    } 

    public void unregister(Object object) { 
     try { 
      this.anyThreadBus.unregister(object); 
     } catch (Exception e) {} 

     try { 
      this.mainThreadBus.unregister(object); 
     } catch (Exception e) {} 

     try { 
      this.notMainThreadBus.unregister(object); 
     } catch (Exception e) {} 
    } 

    public void post(Object event) { 
     this.anyThreadBus.post(event); 
     this.enforceOnMainThread(event); 
     this.enforceOnNotMainThread(event); 
    } 

    public void post(Object event, Strategy strategy) { 
     switch (strategy) { 
      case Main: 
       this.enforceOnMainThread(event); 
       break; 

      case NotMain: 
       this.enforceOnNotMainThread(event); 
       break; 

      case Any: 
      default: 
       this.anyThreadBus.post(event); 
     } 
    } 

    private void enforceOnNotMainThread(final Object event) { 
     if (MyEventBus.onMainThread()) { 
      // MyApplication.pool() returns a shared thread pool for the application 
      MyApplication.pool().execute(new Runnable() { 
       @Override 
       public void run() { 
        notMainThreadBus.post(event); 
       } 
      }); 
     } else { 
      this.notMainThreadBus.post(event); 
     } 
    } 

    private void enforceOnMainThread(final Object event) { 
     if (MyEventBus.onMainThread()) { 
      this.mainThreadBus.post(event); 
     } else { 
      this.mainThreadHandler.post(new Runnable() { 
       @Override 
       public void run() { 
        mainThreadBus.post(event); 
       } 
      }); 
     } 
    } 

    private static boolean onMainThread() { 
     return Looper.myLooper() == Looper.getMainLooper(); 
    } 
} 

我有两个问题:

  1. 在我的岗位我的方法张贴在所有3路公交车的事件,那是因为我不知道是否张贴事件有一个注册类在一定的总线。有什么方法可以知道吗?这样的事情:
    if (this.anyThreadBus.has(event)) { ... } 有没有办法做到这一点,而不是维护每个总线的注册类的事件类地图?

  2. 当前每个注册的类每个调用的执行次数是register。但如果我不能指定每种方法的强制执行而不是整个类的强制执行会是最好的,例如:
    @Subscribe(enforcement = Strategy.Main) public void handleMyEvent(MyEvent event) { ... }
    可以这样做吗?

谢谢。

回答