title: How to link Retrofit and Repository/ViewModel for a MVVM pattern?
body: <p>I have trouble linking Retrofit with MVVM architecture. Indeed, after reading documentation where they only talk about Room which is for SQLite local database, I searched the same but for data that comes from a Rest Server.
  So, I tried to do something similar to this and it doesn't worked :
  <a href="https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-1-604f50cda1" rel="nofollow noreferrer">https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-1-604f50cda1</a></p>
  
  <p>I have an Activity that observes a ViewModel :</p>
  
  <p>Activity code :</p>
  
  <pre><code>mFlightPlanViewModel = ViewModelProviders.of(this).get(FlightPlanViewModel.class);
          mFlightPlanViewModel.getFlightPlans().observe(this, (flightPlans) -&gt; {
              Log.d(TAG, "ON_CHANGED");
  
              mFlightPlanAdapter.setFlightPlans(flightPlans);
          });
  </code></pre>
  
  <p>The ViewModel :</p>
  
  <pre><code>public class FlightPlanViewModel extends AndroidViewModel {
      private static final String TAG = "FlightPlanViewModel";
  
      private LiveData&lt;List&lt;FlightPlan&gt;&gt; mFlightPlans;
      private FlightPlanRepository mFlightPlanRepository;
  
      public FlightPlanViewModel(@NonNull Application application) {
          super(application);
          Log.d(TAG, "CONSTRUCTOR");
  
          mFlightPlanRepository = FlightPlanRepository.getInstance();
          mFlightPlans = mFlightPlanRepository.getFlightPlans();
      }
  
      public LiveData&lt;List&lt;FlightPlan&gt;&gt; getFlightPlans() {
          Log.d(TAG, "GET_FLIGHT_PLANS");
  
          return mFlightPlans;
      }
  }
  </code></pre>
  
  <p>The ViewModel answers the Repository which uses a singleton pattern :</p>
  
  <pre><code>public class FlightPlanRepository {
      private static final String TAG = "FlightPlanRepository";
  
      private static FlightPlanRepository instance;
      private RestApi mRestApi;
  
      private FlightPlanRepository() {
          Log.d(TAG, "CONSTRUCTOR");
  
          mRestApi = RestDao.getRestDao();
      }
  
      public static FlightPlanRepository getInstance() {
          Log.d(TAG, "GET_INSTANCE");
  
          if (instance == null) {
              instance = new FlightPlanRepository();
          }
          return instance;
      }
  
      public MutableLiveData&lt;List&lt;FlightPlan&gt;&gt; getFlightPlans() {
          Log.d(TAG, "GET_FLIGHT_PLANS");
  
          final MutableLiveData&lt;List&lt;FlightPlan&gt;&gt; data = new MutableLiveData&lt;&gt;();
  
          mRestApi.getFlightPlanList().enqueue(new Callback&lt;List&lt;FlightPlan&gt;&gt;() {
              @Override
              public void onResponse(Call&lt;List&lt;FlightPlan&gt;&gt; call, Response&lt;List&lt;FlightPlan&gt;&gt; response) {
                  if (response.code() == 200) {
                      List&lt;FlightPlan&gt; temp = response.body();
                      for (FlightPlan flightPlan : temp) {
                          Log.d(TAG + "res", flightPlan.toString());
                      }
                      data.setValue(response.body());
                      Log.d(TAG + "res", response.toString());
                  }
              }
  
              @Override
              public void onFailure(Call&lt;List&lt;FlightPlan&gt;&gt; call, Throwable t) {
                  List&lt;FlightPlan&gt; flightPlans = new ArrayList&lt;&gt;();
                  flightPlans.add(new FlightPlan(0, "Test", 3.551, 50.52, 3.55122, 50.52625));
                  data.setValue(flightPlans);
                  Log.d(TAG, t.getMessage());
              }
          });
  
          return data;
      }
  }
  
  </code></pre>
  
  <p>The repository uses a Retrofit instance :</p>
  
  <pre><code>public class RestDao {
      private static final String BASE_URL = "http://192.168.1.78:8080";
      private static Retrofit instance;
  
      private static Retrofit getInstance() {
          if (instance == null) {
              instance = new Retrofit.Builder()
                      .baseUrl(BASE_URL)
                      .addConverterFactory(GsonConverterFactory.create())
                      .build();
          }
          return instance;
      }
  
      public static RestApi getRestDao() {
          return getInstance().create(RestApi.class);
      }
  }
  </code></pre>
  
  <p>It uses this interface :</p>
  
  <pre><code>public interface RestApi {
      @GET("/plan/list")
      Call&lt;List&lt;FlightPlan&gt;&gt; getFlightPlanList();
  }
  </code></pre>
  
  <p>The part of the code that doesn't work is :</p>
  
  <pre><code>public MutableLiveData&lt;List&lt;FlightPlan&gt;&gt; getFlightPlans() {
      Log.d(TAG, "GET_FLIGHT_PLANS");
  
      final MutableLiveData&lt;List&lt;FlightPlan&gt;&gt; data = new MutableLiveData&lt;&gt;();
  
      mRestApi.getFlightPlanList().enqueue(new Callback&lt;List&lt;FlightPlan&gt;&gt;() {
          @Override
          public void onResponse(Call&lt;List&lt;FlightPlan&gt;&gt; call, Response&lt;List&lt;FlightPlan&gt;&gt; response) {
              if (response.code() == 200) {
                  List&lt;FlightPlan&gt; temp = response.body();
                  for (FlightPlan flightPlan : temp) {
                      Log.d(TAG + "res", flightPlan.toString());
                  }
                  data.setValue(response.body());
                  Log.d(TAG + "res", response.toString());
              }
          }
  
          @Override
          public void onFailure(Call&lt;List&lt;FlightPlan&gt;&gt; call, Throwable t) {
              List&lt;FlightPlan&gt; flightPlans = new ArrayList&lt;&gt;();
              flightPlans.add(new FlightPlan(0, "Test", 3.551, 50.52, 3.55122, 50.52625));
              data.setValue(flightPlans);
              Log.d(TAG, t.getMessage());
          }
      });
  
      return data;
  }
  </code></pre>
  
  <p>This return a null list. I think that I understand why : calling enqueue() method made a request that is in an other Thread so here we return data without waiting for the result.</p>
  
  <p>So my question is how to link Retrofit and my ViewModel?</p>