[问答]EventBus使用问题。
public class ItemListFragment extends ListFragment { private static final String TAG = "ItemListFragment"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate before"); Log.d(TAG, Thread.currentThread().getName()); EventBus.getDefault().register(this); Log.d(TAG, "onCreate after"); }
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onEventMainThread(Event.ItemListEvent listEvent){
//我此处定义了在UI线程执行的回调函数 //按理说,当执行完post以后就执行下面的setListAdapter函数 //但是结果程序报错,说是在不同的线程修改view //然后我输出了Thread Name,结果是和post所在的线程是相同的 //如果我把post操作放在主线程,那么无论是调用 //onEventPostThread还是onEventMainThread函数或者 //onEventBackgroundThread,都是在主线程 //里执行 //也就是说onEventMainThread这个名字不起作用了,他的执行是根据post所在 //的线程来执行 //这是怎么回事啊? //求助!!!! // 还有一点,就是在程序里必须在回调函数上面加上@Subscribe // 不然程序也会报错,说没有为相应的eventType注册响应函数 //这又是神马情况, //我用的是 compile 'de.greenrobot:eventbus:3.0.0-beta1'
Log.d(TAG, "onEventMainThread before");
Log.d(TAG, Thread.currentThread().getName());
setListAdapter(new ArrayAdapter<Item>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, listEvent.getItems()));
Log.d(TAG, "onEventMainThread after");
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// 如果把post操作放在主线程无论是onEventPostThread还是 //onEventMainThread 或者 //onEventBackgroundThread函数都可以了 // EventBus.getDefault().post(new Event.ItemListEvent(Item.items));
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2000);
Log.d(TAG, "onViewCreated before");
Log.d(TAG, Thread.currentThread().getName());
//在此处另开一个线程来执行post操作 EventBus.getDefault().post(new Event.ItemListEvent(Item.items)); Log.d(TAG, "onViewCreated after"); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); }
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
EventBus.getDefault().post(getListView().getAdapter().getItem(position));
}
}
我也遇到同样的问题,打印线程ID确实不一样,建议使用compile 'de.greenrobot:eventbus:2.4.0'这个版本,这个版本一点问题都没有,不需要@Subscribe,无所谓线程,在哪里post都OK,要不你先替换一下!
@ljnalex 今天我尝试调试的时候,更扯的事发生了,我用onEventBackgroundThread的时候,我添加断点一步一步的来执行结果他居然正常的显示出来了,而且打印出来的线程ID还不一样,也就是说在setListAdapter执行前打印的ID并不是主线程ID,但同时,如果我不调试,直接运行的话又不行了。 但我想setListAdapter这个操作必须是在创建这个view 的线程中进行执行,然后我就搞不懂了。准备再进行调试,看看他到底是在哪个线程里执行的。
我也发现onEventMainThread不一定运行在主线程,通过打印线程ID,发现在哪个线程发送post消息,onEventMainThread就在哪个线程运行。要是这样岂不是无意义了?线程间不能通信。使用的是最新的3.0的beta1的jar包。
@pengkshu 我开始也是用的最新版本的包,那天抽点时间调试,但还时没找到具体原因,等这两天忙过了再好好研究下。或者直接向原作者提问。。
EventBus 的 2.4 版本的回调函数是通过反射查找以 onEvent 开头的方法,3.0 以后作者貌似转为使用注解来实现,所以必须在回调函数上面加上@Subscribe 注解。另外现在最新版不稳定不建议使用。
csdn上的一篇博文写到:
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。 onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。 onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。 onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
@Subscribe(threadMode = ThreadMode.MainThread) 使用这个,就没问题了.
所以结论是什么?该怎么改?
使用EventBus 3 最新版,按照 @binwin20 的姿势使用,没有问题的 :smile: