title: loadInBackground finished before retrofit response
body: <p>I'm having some trouble returning some data from a loader using asynctask.</p>
  
  <p>Using some logs I was able to see that the loadInBackground is being called, it calls the retrofit to load the JSON, and exit calling deliverResult. the point is that when it exit the load has not finished so it passes an empty list to deliverResult, and i get a onLoadFinished with list size 0 too.</p>
  
  <p>Why it is not waiting the retrofit call and then calling deliverResult with the loaded data?</p>
  
  <p>the log (the last one is the response from retrofit with the actual data, but at this time it already called the deliverResult):</p>
  
  <p>08-07 22:10:57.309 6872-6872/com.example.butterknife V/RAG: onCreateLoader()
  08-07 22:10:57.315 6872-6872/com.example.butterknife V/RAG: GetRecipeAsyncTask onStartLoading():</p>
  
  <p>08-07 22:10:57.413 6872-6889/com.example.butterknife V/RAG: loadInBackground list size:0</p>
  
  <p>08-07 22:10:58.533 6872-6872/com.example.butterknife V/RAG: deliverResult data size:0
      onLoadFinished() data size:0</p>
  
  <p>08-07 22:10:59.734 6872-6872/com.example.butterknife V/RAG: loadInBackground inside retrofit success():4</p>
  
  <p>thanks for your time.</p>
  
  <p>github here: <a href="https://github.com/rag-lab/ButterKnifeLab" rel="nofollow noreferrer">https://github.com/rag-lab/ButterKnifeLab</a></p>
  
  <p>the fragment that has it all:</p>
  
  <pre><code>package com.example.rodrigoaugusto.butterknife;
  
  import android.content.Context;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.Fragment;;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.LoaderManager;
  import android.support.v4.content.AsyncTaskLoader;
  import android.support.v4.content.Loader;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.zip.Inflater;
  
  import butterknife.BindView;
  import butterknife.ButterKnife;
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class FragmentRecipe extends Fragment implements android.support.v4.app.LoaderManager.LoaderCallbacks&lt;List&lt;Recipes&gt;&gt;{
  
      View v;
      private RecyclerView myrecview;
      private List&lt;Recipes&gt; lstRecipes = new ArrayList&lt;&gt;();
      private static final int thumbLoaderID= 22;
      private Bundle queryBundle = new Bundle(); //usado no loader das recipes
      private static final String SEARCH_URL = ""; //chave do bundle
  
      //@BindView(R.id.listRecipes_recView) RecyclerView myrecview;
  
  
      public FragmentRecipe() {
      }
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
  
          //ButterKnife.bind(container);
  
          v = inflater.inflate(R.layout.fragment_listrecipe, container, false);
  
          myrecview = (RecyclerView) v.findViewById(R.id.listRecipes_recView);
  
          Recipe_RV_Adapter listRecipe_recViewAdapter = new Recipe_RV_Adapter(container.getContext(),lstRecipes);
  
          myrecview.setLayoutManager(new LinearLayoutManager(getActivity()));
          myrecview.setAdapter(listRecipe_recViewAdapter);
  
          return v;
  
      }
  
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
          LoaderManager loaderManager = getLoaderManager();
          Loader&lt;String&gt; thumbsLoader = loaderManager.getLoader(thumbLoaderID);
  
          queryBundle.putString(SEARCH_URL, Api.BASEURL);
  
          if (thumbsLoader == null) {
              loaderManager.initLoader(thumbLoaderID, queryBundle, this);
          } else {
              loaderManager.restartLoader(thumbLoaderID, queryBundle, this);
          }
  
  
      }
  
  
      /* method not being used*/
      private void getRecipes() {
  
          /*
          Steps stepItem = new Steps("12", "short desc", "description", "videourl", "thumbUrl");
          Steps[] stepItens = {stepItem,stepItem};
  
          Ingredients ingredient = new Ingredients("measure", "ingredient", "qtd");
          Ingredients[] ingredients = {ingredient,ingredient};
  
  
          lstRecipes.add(new Recipes("id","servings","name1","image", stepItens, ingredients));
          lstRecipes.add(new Recipes("id","servings","name2","image", stepItens, ingredients));
          lstRecipes.add(new Recipes("id","servings","name3","image", stepItens, ingredients));
          */
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(Api.BASEURL)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
  
          Api api = retrofit.create(Api.class);
  
          Call&lt;List&lt;Recipes&gt;&gt; call = api.getRecipes();
          call.enqueue(new Callback&lt;List&lt;Recipes&gt;&gt;() {
  
              @Override
              public void onResponse(Call&lt;List&lt;Recipes&gt;&gt; call, Response&lt;List&lt;Recipes&gt;&gt; response) {
  
                  //Snackbar snackbar = Snackbar
                  //      .make(layout, "success", Snackbar.LENGTH_LONG);
                  //snackbar.show();
                  //lstRecipes = response.body();
  
                  List&lt;Recipes&gt; recipes = response.body();
  
                  for(Recipes r: recipes){
  
                      Recipes tmp = new Recipes(r.getId(), r.getServings(), r.getName(), r.getImage(), r.getSteps(), r.getIngredients());
                      lstRecipes.add(tmp);
                  }
  
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;Recipes&gt;&gt; call, Throwable t) {
  
                  Log.v("RAG", "erro:"+t.toString());
  
                  /*
                  Snackbar snackbar = Snackbar
                          .make(layout, t.getMessage(), Snackbar.LENGTH_LONG);
                  snackbar.show();
                  */
              }
  
          });
  
      }
  
  
      //
      //LOADER
      //
      @Override
      public Loader&lt;List&lt;Recipes&gt;&gt; onCreateLoader(int id, final Bundle args) {
  
          Log.v("RAG", "onCreateLoader()");
          return new GetRecipeAsyncTask(getContext());
  
      }
  
      @Override
      public void onLoadFinished(Loader&lt;List&lt;Recipes&gt;&gt; loader, List&lt;Recipes&gt; data) {
  
          Log.v("RAG", "onLoadFinished() data size:" + data.size());
      }
  
  
      @Override
      public void onLoaderReset(Loader&lt;List&lt;Recipes&gt;&gt; loader) {
          //Log.v("RAG", "onLoaderReset()");
      }
  
  
      /*
      //END LOADER
      */
  
  
  
      static class GetRecipeAsyncTask extends AsyncTaskLoader&lt;List&lt;Recipes&gt;&gt;
      {
  
          List&lt;Recipes&gt; tmpLstRecipes;
  
          public GetRecipeAsyncTask(Context context) {
              super(context);
          }
  
  
          @Override
          protected void onStartLoading() {
  
              /*
              super.onStartLoading();
              forceLoad();
              */
  
              //if (args == null) return;
              Log.v("RAG", "GetRecipeAsyncTask onStartLoading():");
  
              //pega do cache ou carrega
              if (tmpLstRecipes != null) {
                  deliverResult(tmpLstRecipes);
              } else {
                  this.forceLoad();
              }
  
          }
  
  
          @Override
          public List&lt;Recipes&gt; loadInBackground() {
  
              tmpLstRecipes = new ArrayList&lt;&gt;();
  
              try {
  
                  Retrofit retrofit = new Retrofit.Builder()
                          .baseUrl(Api.BASEURL)
                          .addConverterFactory(GsonConverterFactory.create())
                          .build();
  
                  Api api = retrofit.create(Api.class);
  
                  Call&lt;List&lt;Recipes&gt;&gt; call = api.getRecipes();
                  call.enqueue(new Callback&lt;List&lt;Recipes&gt;&gt;() {
  
                      @Override
                      public void onResponse(Call&lt;List&lt;Recipes&gt;&gt; call, Response&lt;List&lt;Recipes&gt;&gt; response) {
  
                          //Snackbar snackbar = Snackbar
                          //      .make(layout, "success", Snackbar.LENGTH_LONG);
                          //snackbar.show();
                          //lstRecipes = response.body();
  
                          List&lt;Recipes&gt; recipes = response.body();
  
                          for(Recipes r: recipes){
                              Recipes tmp = new Recipes(r.getId(), r.getServings(), r.getName(), r.getImage(), r.getSteps(), r.getIngredients());
                              tmpLstRecipes.add(tmp);
                          }
  
                          Log.v("RAG", "loadInBackground inside retrofit success():"+ tmpLstRecipes.size());
  
                      }
  
                      @Override
                      public void onFailure(Call&lt;List&lt;Recipes&gt;&gt; call, Throwable t) {
  
                          Log.v("RAG", "loadInBackground onFailure():"+ t.toString());
                          /*
                          Snackbar snackbar = Snackbar
                                  .make(layout, t.getMessage(), Snackbar.LENGTH_LONG);
                          snackbar.show();
                          */
                      }
  
                  });
  
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
              Log.v("RAG", "loadInBackground list size:"+tmpLstRecipes.size());
  
              return tmpLstRecipes;
          }
  
  
  
          @Override
          public void deliverResult(List&lt;Recipes&gt; data) {
  
              Log.v("RAG", "deliverResult data size:"+data.size());
  
  
              // Hold a reference to the old data so it doesn't get garbage collected.
              // We must protect it until the new data has been delivered.
              List&lt;Recipes&gt; oldData = tmpLstRecipes;
              tmpLstRecipes = data;
  
              if (isStarted()) {
                  // If the Loader is in a started state, deliver the results to the
                  // client. The superclass method does this for us.
                  super.deliverResult(data);
              }
  
              // Invalidate the old data as we don't need it any more.
              if (oldData != null &amp;&amp; oldData != data) {
                  //releaseResources(oldData);
              }
  
          }
  
      }
  
  }
  </code></pre>