使用 Retrofit 使用 LiveData 和 Repository 设置 ViewModel 的正确方法
Proper way to setup ViewModel with LiveData and Repository with Retrofit
目前我使用一项服务通过 retrofit
进行网络调用,但我想更新此模型以使用 ViewModel
和 LiveData
。我正在努力解决的问题是如何设置 Repository
来更新实时数据对象。
在我看到的示例中,人们 return 存储库中的 LiveData 包装对象类似于这样
public LiveData<NewsResponse> getData(){
final MutableLiveData<DataResponse> data = new MutableLiveData<>();
apiService.getData().enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response){
if (response.isSuccessful()){
data.setValue(response.body());
}
}
@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
data.setValue(null);
}
});
return data;
}
然后在 ViewModel
他们会做
private MutableLiveData<DataResponse> dataResponse = new MutableLiveData();
private Repository repository;
public PopularGamesViewModel(@NonNull Application application) {
repository = new Repository();
dataResponse = repository.getData();
}
public MutableLiveData<DataResponse> getData(){
return dataResponse;
}
然后在 Activity
他们会做的地方
viewModel.getData().observe(this, dataResponse -> {
if (dataResponse != null)
{
// Do something
}
});
对我来说似乎不对劲的是,每当我想从存储库中获取 new/updated 数据时,都会创建一个新的 LiveData
对象,这样以前的观察者就不再工作了,所以我也会必须重新设置观察者对吗?
您如何设置它以便您不断观察 LiveData
对象,然后从 ViewModel
调用存储库以获取任何新数据,然后 ViewModel
更新来自 Repository
的 LiveData
对象?
我的建议有意义吗?
据我了解你的问题,你想观察实时数据的变化。为此,您可能需要参考 MediatorLiveData。
public LiveData<PagedList<FooPojoList>> liveData;
private MediatorLiveData<PagedList<FooPojoList>> mediatorLiveData;
public FooListViewModel(@NonNull Application application) {
super(application);
mediatorLiveData=new MediatorLiveData<>();
}
public MediatorLiveData<PagedList<FooPojoList>> init(FooDao fooeDao,FooFrom foofrom,String orderBy) {
liveData = new LivePagedListBuilder(fooDao.getAllFooList(sqliteQuery), PAGE).build();
mediatorLiveData.addSource(liveData, new Observer<PagedList<FooPojoList>>() {
@Override
public void onChanged(@Nullable PagedList<FooPojoList> fooListPojos) {
mediatorLiveData.setValue(fooListPojos);
}
});
return mediatorLiveData;
}
目前我使用一项服务通过 retrofit
进行网络调用,但我想更新此模型以使用 ViewModel
和 LiveData
。我正在努力解决的问题是如何设置 Repository
来更新实时数据对象。
在我看到的示例中,人们 return 存储库中的 LiveData 包装对象类似于这样
public LiveData<NewsResponse> getData(){
final MutableLiveData<DataResponse> data = new MutableLiveData<>();
apiService.getData().enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response){
if (response.isSuccessful()){
data.setValue(response.body());
}
}
@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
data.setValue(null);
}
});
return data;
}
然后在 ViewModel
他们会做
private MutableLiveData<DataResponse> dataResponse = new MutableLiveData();
private Repository repository;
public PopularGamesViewModel(@NonNull Application application) {
repository = new Repository();
dataResponse = repository.getData();
}
public MutableLiveData<DataResponse> getData(){
return dataResponse;
}
然后在 Activity
他们会做的地方
viewModel.getData().observe(this, dataResponse -> {
if (dataResponse != null)
{
// Do something
}
});
对我来说似乎不对劲的是,每当我想从存储库中获取 new/updated 数据时,都会创建一个新的 LiveData
对象,这样以前的观察者就不再工作了,所以我也会必须重新设置观察者对吗?
您如何设置它以便您不断观察 LiveData
对象,然后从 ViewModel
调用存储库以获取任何新数据,然后 ViewModel
更新来自 Repository
的 LiveData
对象?
我的建议有意义吗?
据我了解你的问题,你想观察实时数据的变化。为此,您可能需要参考 MediatorLiveData。
public LiveData<PagedList<FooPojoList>> liveData;
private MediatorLiveData<PagedList<FooPojoList>> mediatorLiveData;
public FooListViewModel(@NonNull Application application) {
super(application);
mediatorLiveData=new MediatorLiveData<>();
}
public MediatorLiveData<PagedList<FooPojoList>> init(FooDao fooeDao,FooFrom foofrom,String orderBy) {
liveData = new LivePagedListBuilder(fooDao.getAllFooList(sqliteQuery), PAGE).build();
mediatorLiveData.addSource(liveData, new Observer<PagedList<FooPojoList>>() {
@Override
public void onChanged(@Nullable PagedList<FooPojoList> fooListPojos) {
mediatorLiveData.setValue(fooListPojos);
}
});
return mediatorLiveData;
}