title: ViewModel when implementing Room in Foreground Service
body: <p>I currently have an app with a ForegroundService for all server/API interactions, and a Room database for local persistence. I have been trying to implement an AndroidViewModel to help with data persistence and quick UI refreshes. </p>
  
  <p>However, as per the documentation, <a href="https://stackoverflow.com/questions/48716548/android-viewmodel-inside-service-alternative">ViewModels can't be implemented in Services</a>, and so far I've used the Service to update information locally and notify components using LocalBroadcasts (which is what I want to eliminate using ViewModels and Observers). </p>
  
  <p>I need to have the service running as the app needs to keep running in the background (its a mission critical app and the app getting closed means that the user will not be available to provide critical services), and update certain information on a periodic basis (nearby requests, etc. ).</p>
  
  <p>So to ask the core question - </p>
  
  <ol>
  <li>How do I separate the service from the ViewModel, and if the service has the latest synced data from the servers, how do I update the (Mutable)LiveData lists in my ViewModel?</li>
  <li><a href="https://medium.com/google-developers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54" rel="nofollow noreferrer">This article</a> and <a href="https://stackoverflow.com/questions/44204978/how-to-update-livedata-of-a-viewmodel-from-background-service-and-update-ui">this answer to a question here on SO</a>  says it is better to separate the ViewModel from the Repository while <a href="https://android.jlelse.eu/android-architecture-components-room-livedata-and-viewmodel-fca5da39e26b" rel="nofollow noreferrer">this other</a> one gives an example of including the Room database inside the ViewModel. Which is the better option?</li>
  </ol>
  
  <p>Some of my ViewModel code is as follows:</p>
  
  <pre><code> public class HouseCallViewModel extends AndroidViewModel {
  
          private String TAG = HouseCallViewModel.class.getSimpleName();
  
          private MutableLiveData&lt;List&lt;HouseCall&gt;&gt; housecallList;
          private MutableLiveData&lt;List&lt;HouseCall&gt;&gt; openHousecalls, confirmedHousecalls, closedHousecalls, missedHousecalls, userCancelledHousecalls, respCancelledHousecalls;
          private MutableLiveData&lt;List&lt;Incident&gt;&gt; incidentList, openIncidents;
          private MutableLiveData&lt;List&lt;Incident&gt;&gt; closedIncidents, usercancelIncidents, respcancelIncidents;
          RevivDatabase database;
          Context context;
  
  
          public HouseCallViewModel(Application application) {
              super(application);
  
              //      DANGER WILL ROBINSON                                            
              context = application.getApplicationContext();
              database = Room.databaseBuilder(this.getApplication(),
                      RevivDatabase.class, application.getResources().getString(R.string.database)).build();
          }
          public LiveData&lt;List&lt;HouseCall&gt;&gt; getHousecallList() {
                  if (housecallList == null) {
                      housecallList = new MutableLiveData&lt;&gt;();
                      loadHousecalls(); // need to call API and sync
                  }
                  return housecallList;
              }
         public LiveData&lt;List&lt;HouseCall&gt;&gt; getIncidentList() {
                      if (incidentList == null) {
                          incidentList = new MutableLiveData&lt;&gt;();
                          loadIncidents(); // need to call API and sync
                      }
                      return housecallList;
                  }
  
      // other constructors, getters and setters here, and functions to update the data
      }
  </code></pre>