LiveData - 第二次调用时没有获取片段中的数据
LiveData - not getting data in fragment on second call
我正在使用 LiveData 从服务器获取数据。
在 onResume 方法中,每隔 5 秒调用相同的函数
我只能在第一次 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;
}
无论何时调用 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;
}
在您的虚拟机中:
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 实现的设计可以大大简化。我会敦促检查一些示例!
您正在使用 postDelayed()
两次,因此它不起作用。将您的 onResume()
代码更改为下面提到的。
@Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel.getDetailsResponse("token", "ids");
liveHandler.post(runnable);
}
}, delay);
}
我正在使用 LiveData 从服务器获取数据。 在 onResume 方法中,每隔 5 秒调用相同的函数 我只能在第一次 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;
}
无论何时调用 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;
}
在您的虚拟机中:
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 实现的设计可以大大简化。我会敦促检查一些示例!
您正在使用 postDelayed()
两次,因此它不起作用。将您的 onResume()
代码更改为下面提到的。
@Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel.getDetailsResponse("token", "ids");
liveHandler.post(runnable);
}
}, delay);
}