MVVM communication between Service and ViewModel

Viewed 910

I have a Service that loads data from via http request every second.

I am wondering how to implement the service in my current MVVM structure.

A solution I could think of is to:

  1. View calls method from ViewModel
  2. ViewModel calls method from Repository
  3. Start the Service in the Repository and pass in some LiveData, as Repositories should be responsible for data loading.
  4. Update this LiveData in the Service via postValue().
  5. Observe this LiveData in UI and notify the ViewModel of the changes
  6. In ViewModel get the changed LiveData and update the other LiveData for the View accordingly
  7. Stop the Service based on the outcome of step 6

However, I am wondering if there is no better solution with a more direct communication from Service to ViewModel or even from Service to Repository to ViewModel

0 Answers
Related