在自定义Android服务上观察可观察

时间:2019-12-10 20:57:37

标签: android mvvm service rx-java2 dagger-2

我想观察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);

0 个答案:

没有答案