Ily*_*sis 11 android android-service android-livedata
我有一个存储库,它包含LiveData对象,并由活动和前台服务通过ViewModel使用.当我开始观察活动时,一切都按预期工作.但是,从服务中观察不会触发观察.这是我使用的代码
class MyService: LifecycleService() {
lateinit var viewModel: PlayerServiceViewModel
override fun onCreate() {
viewModel = MyViewModel(applicationContext as Application)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
viewModel.getLiveData().observe(this, Observer { data ->
// Do something with the data
})
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它不起作用,我没有收到数据?
adi*_*e49 16
我曾经ViewModel用LiveData在LifecycleActivity和Fragments它的工作原理,并预期观察数据.
来到你的问题,当你创建新 ViewModel的Service或任何其他Activity它会创建新实例的所有LiveData的,并且在需要其他依赖ViewModel从信息库,并最终DAO查询.如果您没有为两个ViewModel使用相同的DAO,则LiveData可能无法更新,因为它在不同的DAO实例上进行观察.
我Dagger2在我的项目中使用了为DAO和其他常见依赖项维护Singleton实例.因此,您可以尝试创建Repository和DAO 单例以使其在整个Application中保持一致.
我尝试过使用Services与LifecycleService具有相同的流量,它为我工作.
当数据从null变为pull数据时,我得到了以下输出
D/ForegroundService: onStartCommand: Resource{status=LOADING, message='null', data=null}
D/ForegroundService: onStartCommand: Resource{status=SUCCESS, message='null', data=TVShow(id=14,...
Run Code Online (Sandbox Code Playgroud)
首先它显示空数据,因为数据库中没有数据从网络中提取数据并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);
}
}
Run Code Online (Sandbox Code Playgroud)