doInBackground跑那么快片段中使用的AsyncTask时 [英] doInBackground run so fast when using AsyncTask in Fragment

查看:122
本文介绍了doInBackground跑那么快片段中使用的AsyncTask时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段

的onCreate 片段我叫的AsyncTask 像所以:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Step 1 
    new FitnessHistoryDataAsync(getActivity()).execute(10);

    super.onCreate(savedInstanceState);
}

doInBackground 方法:

@Override
protected Boolean doInBackground(Integer... params) {
    // Step 2 
    Log.i("", "doInBackground");

    Constants.SDK.getFitnessHistoryData(context, 10,
            new FitnessHistoryListener() {

        @Override
        public void onReceiveFailed(String errorMessage) {
             // Step 4 
            Log.i("", "error " + errorMessage);
        }

        @Override
        public void onReceiveData(
                FitnessHistory[] fitnessHistoryData) {
            // Step 4 
            Log.i("", "onReceiveData");
        }
    });

    return true;
}

onPostExecute 方法:

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);

    // Step 3 
    Log.i("", "onPostExecute");

            // Hide progress bar dialog
    Constants.connectivity.hideProgressDialog();
}

我的的AsyncTask 跑如下:第1步第2步第3步,那么第4步

问题是在 doInBackground 方法,其中第2步第4步不能在同一时间第3步 onPostExecute 方法。

The problem is in the doInBackground method where Step 2 and Step 4 cannot run at the same time before running Step 3 in the onPostExecute method.

我想在 doInBackground 法的一切才去 onPostExecute 方法来运行。

I want everything in the doInBackground method to run before going to the onPostExecute method.

请帮我。

感谢。

编辑:

我改成下面的code:

I changed to the following code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // Step 1 
    new FitnessHistoryDataAsync(getActivity()).executeonExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 10);
}

虽然我用 AsyncTask.THREAD_POOL_EXECUTOR AsyncTask.SERIAL_EXECUTOR 来尝试解决这个问题,但不能。

Although I used AsyncTask.THREAD_POOL_EXECUTOR or AsyncTask.SERIAL_EXECUTOR to try to fix this problem, but cannot.

推荐答案

它看起来像我以前也是另一个线程库。
所以,我没有必要建立新的线程的AsyncTask

It looks like the library I used also is another thread. So, I no need to create new thread AsyncTask.

只要编辑编码是这样的:

Just edit coding like this :

Constants.SDK.getFitnessHistoryData(context, 10,
        new FitnessHistoryListener() {

    @Override
    public void onReceiveFailed(String errorMessage) {
         // Step 4 
        Log.i("", "error " + errorMessage);
    }

    @Override
    public void onReceiveData(
            FitnessHistory[] fitnessHistoryData) {
        // Step 4 
        Log.i("", "onReceiveData");

        // TODO Directly updated UI in here when already received new data
    }
});

这解决了我的问题。

That's solved my issue.

这篇关于doInBackground跑那么快片段中使用的AsyncTask时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆