我ViewModel
与LiveData
in一起使用LifecycleActivity
,Fragments
它按预期工作并观察数据。
遇到您的问题,当您从或任何其他创建新实例时,它会创建从 存储库和最终 DAO 查询所需的所有 LiveData 和其他依赖项的新实例。如果您没有对两个 ViewModel 使用相同的 DAO,则您可能不会更新,因为它正在观察不同的 DAO 实例。ViewModel
Service
Activity
ViewModel
LiveData
我Dagger2
在我的项目中使用来维护 DAO 和其他常见依赖项的 Singleton 实例。因此,您可以尝试使您的 Repository 和 DAO单例,以使其在整个应用程序中保持一致。
我尝试使用 with Services
withLifecycleService
和相同的流程,它对我有用。
当数据从 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);
}
}