Android:AsyncTask需要很长时间才能从doInBackground返回到postExecute

时间:2015-09-08 03:36:51

标签: java android android-asynctask

我在片段中运行AsyncTask并且需要很长时间(1-3秒)才能从doInBackground中的return语句转到postExecute的第一行。

我设置了日志,这使我得出了这个结论:

  • 09-07 20:26:15.496:输入myAsyncTask的doInBackground

  • 09-07 20:26:15.496 :完成myAsyncTask的doInBackground

  • 09-07 20:26:18.524 :输入myAsyncTask的postExecute

  • 09-07 20:26:18.525:完成myAsyncTask的postExecute

我不明白为什么要花这么长时间才开始postExecute。在doInBackground我只是返回null,所以传递该参数不应该是耗时的。我不明白为什么这么长时间。

有人可以为我澄清一下吗?为什么从postExecute的返回行输入doInBackground需要这么长时间?

下面的AsyncTask代码:

 public class UpdateTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected void onPreExecute() {
        Log.d("AnswerTabFragment", "Entered preExecute of UpdateTask");

        mRefreshWrapper.setRefreshing(true);
    }

    @Override
    protected void onPostExecute(Void param) {
        Log.d("AnswerTabFragment", "Entered postExecute of UpdateTask");

        mRefreshWrapper.setRefreshing(false);
        if(mAnswers.isEmpty()){
            mHeaderTextView.setText("No Answers");
            mHeaderTextView.setVisibility(View.VISIBLE);
        }
        else{
            mHeaderTextView.setVisibility(View.GONE);
        }
        Log.d("AnswerTabFragment", "Finished postExecute of UpdateTask");

    }

    @Override
    protected Void doInBackground(Void... params  ) {
        Log.d("AnswerTabFragment", "Entered doInBackground of UpdateTask");

        if(u == Application.qollegeCache.currentUser){
            mAnswers.clear();
            mAnswers.addAll(Application.qollegeCache.myAnswers);

        }
        else{
            try {
                Answer.unpinAll(u.getObjectId() + "Answers");

                mAnswers.addAll(Answer.getQuery()
                        .whereEqualTo(Constants.kQollegeAnswerPostedByKey, u)
                        .orderByDescending(Constants.kQollegeQuestionCreatedAtKey)
                        .find());

                Answer.pinAllInBackground(u.getObjectId() + "Answers", mAnswers);


            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        Log.d("AnswerTabFragment", "Finished doInBackground of UpdateTask");

        return null;

    }
}

0 个答案:

没有答案