S.Else
S.Else

Reputation: 47

Wait for completion of AsyncTask and Loading of Map

I am writing a code which takes input from one AsyncTask(HttpUrlConnection) to start another AsyncTask(HttpUrlConnection) which then uses the information to add markers to a Google Map.

The problem I am facing is that map has not loaded at the time when I try to add markers through onPostExecute. But when I try to use onMapReady the data has not yet been downloaded.

Is there a way where I can wait for both the data to load and the map to be ready before attempting to place markers?

Upvotes: 0

Views: 957

Answers (3)

Bonatti
Bonatti

Reputation: 2781

Synce the question is very generic and provided no custom code, I will post a "generic solution":

boolean isStep1Done = false;
boolean isStep2Done = false;

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

    @Override
    protected String doInBackground(String... params) {
        Thread.sleep(1000);
        return "Completed";
    }

    @Override
    protected void onPostExecute(String result) {
        isStep1Done = true;
        continueLogic();
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

Same thing for ExampleOperation 2, where it sets isStep2Done to true. Then, if they are sequential, you can continue, if they are independant, on each you call continueLogic(), and the first step in this function, you check for all flags.

private void continueLogic(){
   if(isStep1Done && isStep2Done ){
      stuffs...
   }
} 

Upvotes: 2

betorcs
betorcs

Reputation: 2831

You can try the following logic.

public class MyAct extends Activity implements OnMapReadyCallback  {

  private GoogleMap mMap;
  private List<YourObj> mItems;
  
  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    populateMap();
  }
  
  private class MyTask extends AsyncTask {
    ...
    protected void onPostExecute(List<YourObj> results) {
      mItems = results;
      populateMap();
    }
  }

  private void populateMap() {
    if (mMap != null && mItems != null) {
      for (YourObj o : mItems) {
        mMap.addMarker(...);
      }
    }
  }
  
}

Upvotes: 0

Stepan Maksymov
Stepan Maksymov

Reputation: 2606

what is the problem use 2 booleans and set them to true when data load done and when map load done and in each of this async tasks add check if both true then launch adding markers )

Upvotes: 0

Related Questions