我正在使用LiveData从服务器获取数据。 在onResume方法中,每隔5秒调用一次相同的函数 我只能在First API调用上获取数据。 第二次观察者没有被触发并且无法获取片段中的数据。
这是我的片段:
private int delay = 5 * 1000;
private ViewModel mViewModel;
private DetailsModel details = new DetailsModel();
mViewModel = ViewModelProviders.of(this).get(ViewModel.class);
mViewModel.getDetailsResponse("token", "ids");
mViewModel.getData().observe(this, new Observer< DetailsModel >() {
@Override
public void onChanged(DetailsModel response) {
details = response;
}});
//getting data in every 5 seconds
@Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel. getDetailsResponse("token", "ids");
liveHandler.postDelayed(runnable, delay);
}
}, delay);
}
ViewModel.java
private MutableLiveData<DetailsModel> detailsResponse;
private ProjectRepository repository = new ProjectRepository();
public void getDetailsResponse(String token, String ids) {
detailsResponse = repository.getMapData("token", "ids");
}
public MutableLiveData<DetailsModel> getData() {
return detailsResponse;
}
ProjectRepository.java
public MutableLiveData<DetailsModel> getMapData(String token, String ids) {
final MutableLiveData<DetailsModel> responseMutableLiveData = new MutableLiveData<>();
Call<DetailsModel> call = service.getMapDetails(token, ids);
call.enqueue(new Callback<DetailsModel>() {
@Override
public void onResponse(@NonNull Call<DetailsModel> call, @NonNull Response<DetailsModel> response) {
responseMutableLiveData.postValue(response.body());
}
@Override
public void onFailure(@NonNull Call<DetailsModel> call, @NonNull Throwable t) {
t.printStackTrace();
}
});
return responseMutableLiveData;
}
答案 0 :(得分:1)
每当调用getDetailsResponse
时,都会创建一个新的LiveData
对象,这是有问题的,您应该在ProjectRepository
中这样做
final MutableLiveData<DetailsModel> responseMutableLiveData = new MutableLiveData<>();
public MutableLiveData<DetailsModel> getMapData(String token, String ids) {
Call<DetailsModel> call = service.getMapDetails(token, ids);
call.enqueue(new Callback<DetailsModel>() {
@Override
public void onResponse(@NonNull Call<DetailsModel> call, @NonNull Response<DetailsModel> response) {
responseMutableLiveData.postValue(response.body());
}
@Override
public void onFailure(@NonNull Call<DetailsModel> call, @NonNull Throwable t) {
t.printStackTrace();
}
});
return responseMutableLiveData;
}
在您的VM中:
private MutableLiveData<DetailsModel> detailsResponse = null;
private ProjectRepository repository = new ProjectRepository();
public void getDetailsResponse(String token, String ids) {
if (detailsResponse == null) {
detailsResponse = repository.getMapData("token", "ids");
} else {
// Just call it, you already assigned before
repository.getMapData("token", "ids");
}
}
public MutableLiveData<DetailsModel> getData() {
return detailsResponse;
}
因此,基本上将对象创建移出函数本身。但是,您的MVVM实现的设计可以大大简化。我敦促检查一些例子!
答案 1 :(得分:0)
您两次使用postDelayed()
,所以它不起作用。将您的onResume()
代码更改为以下内容。
@Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel.getDetailsResponse("token", "ids");
liveHandler.post(runnable);
}
}, delay);
}