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:
View
calls method fromViewModel
ViewModel
calls method fromRepository
- Start the
Service
in theRepository
and pass in someLiveData
, as Repositories should be responsible for data loading. - Update this
LiveData
in theService
viapostValue()
. - Observe this
LiveData
in UI and notify theViewModel
of the changes - In
ViewModel
get the changedLiveData
and update the other LiveData for the View accordingly - 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