【发布时间】:2018-10-02 12:17:19
【问题描述】:
我有一个简单的应用程序,它可以轮询服务以在回收站视图中显示国家/地区列表。在这里,只要国家/地区列表有任何变化,我就会使用 LiveData 更新回收站视图。麻烦的是,LiveData 的 onChanged 方法仅在第一次调用 setValue 时才被调用。但在那之后,如果数据有任何进一步的变化,onChanged 就不会被调用??
以下是我的代码以获取更多信息 -
CountryListFragment
//Setup observer
Observer<List<CountryModel>> myObserver = new Observer<List<CountryModel>>() {
@Override
public void onChanged(@Nullable List<CountryModel> countryModels) {
mCountryList = countryModels;
//Update recyclerview
myAdapter.updateRecyclerView(mCountryList);
}
};
//Set Observer for Viewmodel
countryViewModel.getmCountryList().observe(this,myObserver);
CountryViewModel
公共类 CountryViewModel 扩展 AndroidViewModel { 私有 MutableLiveData> mCountryList; 私人 MyRetrofitClient myRetrofitClient;
public CountryViewModel(@NonNull Application application) {
super(application);
}
public void init(){
mCountryList = new MutableLiveData<>();
myRetrofitClient = new MyRetrofitClient();
**mCountryList = myRetrofitClient.getCountryList(); //This works**
pollCountryList();
}
//Create polling request for every 10 secs
private void pollCountryList(){
final Handler mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0; i<30;i++){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Call API on main thread
mHandler.post(new Runnable() {
@Override
public void run() {
**myRetrofitClient.getCountryList(); //NOT CALLING ONCHANGED??**
}
});
}
}
}).start();
}
public MutableLiveData<List<CountryModel>> getmCountryList() {
return mCountryList;
}
MyRetrofitClient.getCountryList()
public MutableLiveData<List<CountryModel>> getCountryList(){ final MutableLiveData<List<CountryModel>> lstResult = new MutableLiveData<>(); MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class); Call<List<CountryModel>> countryList = serviceInterface.getCountryList(); countryList.enqueue(new Callback<List<CountryModel>>() { @Override public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) { if (response.isSuccessful()){ List<CountryModel> lstResponse = response.body(); lstResult.setValue(lstResponse); }else { System.out.print(response.errorBody()); } }
@Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
t.printStackTrace();
}
});
return lstResult;
}
谢谢!
编辑:
一些额外的观察- 当我在 CountryViewModel 中调用 MutableLiveData 实例 (mCountryList) 的 setValue 方法时,它每次都会调用 onChanged 方法。
但是在 MyRetrofitClient 的情况下它有所不同。在 MyRetrofitClient.getCountryList() 中第一次调用 setValue 时,它会调用 onChanged 方法。但后来就没有了。
【问题讨论】:
标签: android retrofit2 android-livedata