EventBus替代AsyncTask在子线程中处理任务

之前AsyncTask写法:

    new GetTask().execute();
    private class GetTask extends AsyncTask<Void, Void, ArrayList<Channel>> {

        @Override
        protected ArrayList doInBackground(Void... params) {
            doWastingTimeTask();
        }

        @Override
        protected void onPostExecute(ArrayList videos) {
            doPostExecute();
        }
    }

改为EventBus写法:

    EventBus.getDefault().post(new Event(XXX));
    @Subscribe(threadMode = ThreadMode.BACKGROUND, priority = 1)
    public void onEventGet(Event event) {
        if (XXX.equals(event.tag)) {
            doWastingTimeTask();
            doPostExecute();
        }
    }

ThreadMode.BACKGROUND解释:
如果调用post方法的线程不是主线程,则直接在该线程执行;
如果是主线程,则切换到后台单例线程,多个方法公用同个后台线程,按顺序执行,避免耗时操作;

你可能感兴趣的:(框架,EventBus)