hehonghui / AndroidEventBus

A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.
Apache License 2.0
1.61k stars 392 forks source link

AndroidEventBus的优化建议 #48

Open xiaweicheng opened 7 years ago

xiaweicheng commented 7 years ago

(1)EventBus里面的handleStickyEvent方法最后执行的判断条件冗余,因为通过foundEventType找到的subscription其eventType一定和foundType相等。所以判断条件只需要isTarget(subItem, subscriber)即可

(2)经过第一步之后,Subscription里面的EventType就没有被用到,可以去掉了。因为EventType在mSubscribeMap是Key的角色,Subscription本质是value的角色,value里面含有key很奇怪

(3)TargetMethod这个类,感觉有点冗余,这个类只在SubscribeMethodHunter里面使用,它从findSubcribeMethods(...)方法里面接收Method , EventType type, ThreadMode 参数,然后传递给subscibe(EventType event, TargetMethod method, Object subscriber)使用,建议把subscribe(...)方法改成 private void subscibe(EventType event, Object subscriber, Method method, ThreadMode threadMode) ,这样TargetMethod这个类也可以优化掉。

xiaweicheng commented 7 years ago

(4)Subscription的equals()方法在用到了subscriber.get().equals(other.subscriber.get())。这样会导致两个值相等的不同对象,无法同时注册。建议把调用 subscriber.get().equals(other.subscriber.get()) 的地方subscriber.get() == other.subscriber.get()