title: Sequential AsyncTasks where each following use result from previous
body: <p>I thought a bit about this and I was wondering which could be the best pattern to accomplish my request.</p>
  
  <p>Real case:</p>
  
  <p>I have many web calls which must be sequential and where every call must use the result of the previous. This is the concept of what I mean:</p>
  
  <p>Suppose to have this db structure:</p>
  
  <ul>
  <li>User [id, server-side created info, infos]</li>
  <li>House [id, server-side created info, user ref, infos]</li>
  <li>Room [id, server-side created info, house ref, infos]</li>
  </ul>
  
  <p>Suppose that with my app (offline) an user can create his user entity, N house entities and N room entities for each house.</p>
  
  <p>Then, once done, the user is syncing data with server.</p>
  
  <p>This is what should be done:</p>
  
  <ol>
  <li>For each user, send user to webapi and read the server-side info.</li>
  <li>For each house of the sent user, read the server-side info, send the house to webapi and read the server-side info.</li>
  <li>For each room of the sent house, read the server-side info, send the room to webapi and read the server-side info.</li>
  </ol>
  
  <p>This might be done this way:</p>
  
  <ul>
  <li>Create an AtomicInteger that is the count of all sync to be done.</li>
  </ul>
  
  <p>Init this value with the sum of users, houses, rooms.</p>
  
  <pre><code>void syncDone() {
    if(totalSyncCounter.decrementAndGet() == 0){
      //sync finished, prompt user
    }
  }
  </code></pre>
  
  <p>Create AsyncTasks</p>
  
  <pre><code>class SendUserToDbAsyncTask extends AsyncTask&lt;User, Void, Integer&gt; {
  
      UserSavedCallback _callback;
  
      public SendUserToDbAsyncTask(UserSavedCallback _callback) {
          this._callback = _callback;
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected Integer doInBackground(User... users) {
          //save user and return the server-side value
          return serverSideInfo;
      }
  
      @Override
      protected void onPostExecute(Integer res) {
          super.onPostExecute(res);
          _callback.onResult(res);
      }
  }
  
  class SendHouseToDbAsyncTask extends AsyncTask&lt;House, Void, Integer&gt; {
  
      HouseSavedCallback _callback;
  
      public SendHouseToDbAsyncTask(HouseSavedCallback _callback) {
          this._callback = _callback;
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected Integer doInBackground(House... houses) {
          //save user and return the server-side value
          return serverSideInfo;
      }
  
      @Override
      protected void onPostExecute(Integer res) {
          super.onPostExecute(res);
          _callback.onResult(res);
      }
  }
  
  class SendRoomToDbAsyncTask extends AsyncTask&lt;User, Void, Integer&gt; {
  
      RoomSavedCallback _callback;
  
      public SendRoomToDbAsyncTask(RoomSavedCallback _callback) {
          this._callback = _callback;
      }
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
      }
  
      @Override
      protected Integer doInBackground(Room... rooms) {
          //save user and return the server-side value
          return serverSideInfo;
      }
  
      @Override
      protected void onPostExecute(Integer res) {
          super.onPostExecute(res);
          _callback.onResult(res);
      }
  }
  </code></pre>
  
  <p>Logic:</p>
  
  <pre><code>void DoStuffs() {
  
    mySyncCounter = [users + houses + rooms];
    for (User user : users) {
      new SendUserToDbAsyncTask(new UserSavedCallback() {
        void onResult(){
          syncDone();
          for (House house : user.houses) {
            new SendHouseToDbAsyncTask(new HouseSavedCallback() {
              void onResult(){
                syncDone();
                for (Room room : house.rooms) {
                  new SendRoomToDbAsyncTask(new RoomSavedCallback() {
                    void onResult(){
                      syncDone();
                  }).execute(room);
                }
            }).execute(house);
          }
      }).execute(user);
    }
  }
  </code></pre>
  
  <p>Obiouvsly it is just an example hand-wrote here on SO. I just want to make you get the point. I know I can make a single callback for all, init it outside the method, just create a single asynctask for everything, etc... but I'm not looking for any suggestion about optimizing this specific code, I just want to know how to perform multiple sequential async operations where every following operation use the return of the previous.</p>
  
  <p>What is the best way for performing this kind of operations?</p>
  
  <p>Is there a "cleaner" way of doing it?</p>
  
  <p>Thanks</p>