使用嵌套AsyncTasks的最佳做法是什么?

时间:2017-11-28 09:43:23

标签: android android-asynctask nested readability

在我的Android应用程序中,我有多达4个相互依赖的异步任务,这意味着一个任务必须在下一个任务可以使用已检索的数据之前完成。现在,当代码看起来像这样时,这一点可能还不太清楚:

final AsyncTask<Void, Void, Boolean> taskOne = new AsyncTask<Void, Void, Boolean>() {   
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // retrieve required data   
        return true;
    }

    @Override
    protected void onPostExecute(Boolean success) {
        if (success) {
            // start second task here
            final AsyncTask<Void, Void, Boolean> taskTwo = new AsyncTask<Void, Void, Boolean>() {
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }

                @Override
                protected Boolean doInBackground(Void... params) {
                    // retrieve required data   
                    return true;
                }

                protected void onPostExecute(Boolean success) {
                    if (success) {
                        // start third task here
                        final AsyncTask<Void, Void, Boolean> taskThree = new AsyncTask<Void, Void, Boolean>() {
                            @Override
                            protected void onPreExecute() {
                                super.onPreExecute();
                            }

                            @Override
                            protected Boolean doInBackground(Void... params) {
                                // retrieve required data   
                                return true;
                            }

                            protected void onPostExecute(Boolean success) {
                                if (success) {
                                    // and so on ...
                                }
                            }
                        }

                        taskThree.execute();
                    }
                }
            }

            taskTwo.execute();
        }
    }
}

taskOne.execute();

使用更易读的代码实现此行为的最佳做法是什么?

提前致谢

2 个答案:

答案 0 :(得分:2)

<强> TaskOne

Class TaskOne extends AsyncTask{

  onPostExecute(boolean success){
     if(success){
        new TaskTwo().execute();
     }
  }
}

<强> TaskTwo

Class TaskTwo extends AsyncTask{

  onPostExecute(boolean success){
     if(success){
        new TaskThree().execute();
     }
  }
}

<强> TaskThree

Class TaskThree extends AsyncTask{

      onPostExecute(boolean success){
         if(success){
            //do something
         }
      }
}

答案 1 :(得分:1)

我认为你应该使用Interface崩溃。在你的情况下,它被称为Listener或Callback。我没有测试代码。但它看起来像那样

    Class A extend Activity implement ServerRequestListener{


          public ServerRequestListener listener;

          @Override 
          onCreate (Bundle bundle){
               listener = this;
               doTaskOne();
          }

          public void doTaskOne(){
               final AsyncTask<Void, Void, Boolean> taskOne = new AsyncTask<Void, Void, Boolean>() {   
                     @Override
                     protected void onPreExecute() {
                         super.onPreExecute();
                     }

                     @Override
                     protected Boolean doInBackground(Void... params) {
                         // retrieve required data   
                         return true;
                     }

                     @Override
                     protected void onPostExecute(Boolean success) {
                         if (success) {
                             listener.onSuccess(new JSON());
                         }
                     }
            }

            taskOne.execute();

        }



         public interface ServerRequestListener {
               void onSuccess(Json);
               void onFailure(Error);
         }


         @Override
         onSuccess(Json json){
             //call second task
         }

         @Override
         onFailure(Error error){

         }

    }