我想观察ViewModel中的数据,但无法在自定义Android服务上使用runOnUiThread
public void loadData() {
locationViewModel.loadLocation();
locationViewModel.getLocationResults().observe((LifecycleOwner) getApplicationContext(), l->{
l.stream()
.forEach(x->{
viewModel.loadWeather(x.getName());
Log.e("weather service", x.getName());
});
});
locationViewModel.weatherLocationError().observe((LifecycleOwner) getApplicationContext(), e->{Log.e("Weather service", e);});
viewModel.weatherError().observe((LifecycleOwner) getApplicationContext(), e->Log.e("weather serivice", e));
}
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
loadData();
Log.e("service", "Service is running...");
}
}, 0, 5, TimeUnit.SECONDS);