需要设计模式建议:包括AsyncTask

时间:2022-07-17 12:57:13

This is my current situation.

这是我目前的情况。

Class Foo{
    public static boolean isAsyncTask1Done;
    public static boolean isAsyncTask2Done;
    AsyncTask1(); //in onPostExecute make isAsyncTask1Done= true
    AsyncTask2(); //in onPostExecute make isAsyncTask2Done= true
    //execute the following method after the above 2 complete.

    if (isAsyncTask1Done == true && isAsyncTask2Done == true)
        doPostProcessing();
}

Now this class is also an activity. Also since the AsyncTask1(); and AsyncTask2(); will take sometime, I have make them as Async tasks (I might make them as intentservice).

现在这个班也是一个活动。也是因为AsyncTask1();和AsyncTask2();需要一段时间,我已将它们作为异步任务(我可能将它们作为intentservice)。

Now my question if how should I call doPostProcessing() ? I need to call this only when AsyncTask1() and AsyncTask2() are done.

现在我的问题是如何调用doPostProcessing()?我只需要在完成AsyncTask1()和AsyncTask2()时调用它。

Thanks

2 个解决方案

#1


The way you have your example set up, it won't work. The event needs to be triggered by the last AsyncTask completing.

您设置示例的方式,它将无法正常工作。该事件需要由最后一次AsyncTask完成触发。

Start the AsyncTasks:

启动AsyncTasks:

new AsyncTask1.execute();
new AsyncTask2.execute();

Then, when the last one is complete, call doPostProcessing().

然后,当最后一个完成时,调用doPostProcessing()。

Edit: Added synchronized blocks.

编辑:添加同步块。

    private boolean asyncTask1Completed = false;
    private boolean asyncTask2Completed = false;
    private boolean ranPostProcessing = false;

    private class AsyncTask1 extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            //......
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "result";
            //......
            return result;
        }

        @Override
        protected void onPostExecute(String result) {

            asyncTask1Completed = true;

            synchronized(MainActivity.this) {
                if (asyncTask2Completed && !ranPostProcessing) {
                    ranPostProcessing = true;
                    //Call doPostProcessing once both AsyncTasks are complete:
                    doPostProcessing();
                }
            }


        }
    }

    private class AsyncTask2 extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            //......
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "result";
            //......
            return result;
        }

        @Override
        protected void onPostExecute(String result) {

            asyncTask2Completed = true;

            synchronized(MainActivity.this) {
                if (asyncTask1Completed && !ranPostProcessing) {
                    ranPostProcessing = true;
                    //Call doPostProcessing once both AsyncTasks are complete:
                    doPostProcessing();
                }
            }
        }
    }

#2


In PostExcecute of task1 you check if task2 has been done. If so, do postexecute. If not, do nothing. Same for task2 checking if task 1 has been done.

在task1的PostExcecute中,检查task2是否已完成。如果是这样,请执行postexecute。如果没有,什么也不做。任务2检查任务1是否已完成相同。

And you need to .execute() the tasks.

你需要执行.execute()任务。

And I'm sure that if you have a close look at your app, there's better ways to do it than this.

我敢肯定,如果你仔细看看你的应用程序,那么有更好的方法来做到这一点。

#1


The way you have your example set up, it won't work. The event needs to be triggered by the last AsyncTask completing.

您设置示例的方式,它将无法正常工作。该事件需要由最后一次AsyncTask完成触发。

Start the AsyncTasks:

启动AsyncTasks:

new AsyncTask1.execute();
new AsyncTask2.execute();

Then, when the last one is complete, call doPostProcessing().

然后,当最后一个完成时,调用doPostProcessing()。

Edit: Added synchronized blocks.

编辑:添加同步块。

    private boolean asyncTask1Completed = false;
    private boolean asyncTask2Completed = false;
    private boolean ranPostProcessing = false;

    private class AsyncTask1 extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            //......
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "result";
            //......
            return result;
        }

        @Override
        protected void onPostExecute(String result) {

            asyncTask1Completed = true;

            synchronized(MainActivity.this) {
                if (asyncTask2Completed && !ranPostProcessing) {
                    ranPostProcessing = true;
                    //Call doPostProcessing once both AsyncTasks are complete:
                    doPostProcessing();
                }
            }


        }
    }

    private class AsyncTask2 extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            //......
        }

        @Override
        protected String doInBackground(String... params) {
            String result = "result";
            //......
            return result;
        }

        @Override
        protected void onPostExecute(String result) {

            asyncTask2Completed = true;

            synchronized(MainActivity.this) {
                if (asyncTask1Completed && !ranPostProcessing) {
                    ranPostProcessing = true;
                    //Call doPostProcessing once both AsyncTasks are complete:
                    doPostProcessing();
                }
            }
        }
    }

#2


In PostExcecute of task1 you check if task2 has been done. If so, do postexecute. If not, do nothing. Same for task2 checking if task 1 has been done.

在task1的PostExcecute中,检查task2是否已完成。如果是这样,请执行postexecute。如果没有,什么也不做。任务2检查任务1是否已完成相同。

And you need to .execute() the tasks.

你需要执行.execute()任务。

And I'm sure that if you have a close look at your app, there's better ways to do it than this.

我敢肯定,如果你仔细看看你的应用程序,那么有更好的方法来做到这一点。