I used ViewModelwith LiveDatain LifecycleActivityand Fragmentsit works and monitors the data as expected.
ViewModel Service Activity LiveData ViewModel , , DAO.
DAO ViewModels, LiveData , DAO.
Dagger2 Singleton DAO . , DAO singleton, .
Services LifecycleService , .
, null
D/ForegroundService: onStartCommand: Resource{status=LOADING, message='null', data=null}
D/ForegroundService: onStartCommand: Resource{status=SUCCESS, message='null', data=TVShow(id=14,...
,
Observer.
public class ForegroundService extends LifecycleService {
private static final String TAG = "ForegroundService";
private TVShowViewModel tvShowViewModel;
private TVShow tvShow;
@Inject TVShowDataRepo tvShowDataRepo;
@Override
public void onCreate() {
super.onCreate();
AndroidInjection.inject(this);
tvShowViewModel = new TVShowViewModel(tvShowDataRepo);
tvShowViewModel.init(14);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
tvShowViewModel.getTVShow().observe(this, tvShowResource -> {
Log.d(TAG, "onStartCommand: " + tvShowResource);
});
return super.onStartCommand(intent, flags, startId);
}
}