响应的改造MVVM延迟
我在片段中实现了回收器视图,该视图将根据在搜索视图中输入的文本进行更新。在搜索视图中输入的文本很快就在MainAttivity中。我在存储库中调用OnQueryTextChange(EnteredText),并根据输入的文本从API中获取数据。它发生在实时
问题上:
我一旦调用存储库中的QueryTextChange就无法从存储库中返回,该数据将调用API并将数据设置为MutableBableLive数据并返回实时数据。但是我从存储库中获取零值,因为API调用需要一些时间来获取数据。我如何解决这个问题以实现我想要的东西。
我目前正在尝试通过..
MainAttivity ->包含搜索视图
片段 - >包含RecyClerview
MainActivity
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
model.queryTextChange(query,getString(R.string.api_key));
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
model.queryTextChange(newText,getString(R.string.api_key));
return false;
}
});
我的视图模型:
private String API_KEY = "";
// This is observed from fragment
public MutableLiveData<SearchGifs> gifList = new MutableLiveData<>();
public HomeViewModel(String api_key) {
this.API_KEY = api_key;
}
GifRepo gifRepo = new GifRepo();
public void queryTextChange(String text, String key){
//Calls the repo and updates the mutablelivedata which is observed from fragment
gifList.postValue(gifRepo.queryTextChange(text,key).getValue());
}
存储库:
public MutableLiveData<SearchGifs> queryTextChange(String text, String api_key) {
MutableLiveData<SearchGifs> list = new MutableLiveData<>();
Call<SearchGifs> gifSearch = service.search(api_key,text);
gifSearch.enqueue(new Callback<SearchGifs>() {
@Override
public void onResponse(Call<SearchGifs> call, Response<SearchGifs> response) {
//getting data here perfectly
list.postValue(response.body());
}
@Override
public void onFailure(Call<SearchGifs> call, Throwable t) {
}
});
//doesn't wait for api call and returning null
return list;
}
fragment :(在哪里应该更新回收库)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Not Getting triggered
model.gifList.observe(requireActivity(), new Observer<SearchGifs>() {
@Override
public void onChanged(SearchGifs gifs) {
//RecyclerViewData will be updated here
}
});
I implemented a recycler view in a fragment which will be updated based on the text entered in searchview. As soon a text entered in searchview which is in MainActivity. I'm calling onQueryTextChange(enteredText) in repository and fetching data from API based on the text entered. it happens in realtime
The Problem :
I'm unable to return the data fetched from repositoty as soon as i call the queryTextChange in repository which will call the api and set the data to the mutablelive data and returns the live data. but im getting null value from repository as the api call takes some time to fetch the data. How do i solve this issue to achieve what i wanted.
I'm currently trying to achieve this via..
MainActivity --> contains Searchview
Fragment --> contains recyclerview
MainActivity
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
model.queryTextChange(query,getString(R.string.api_key));
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
model.queryTextChange(newText,getString(R.string.api_key));
return false;
}
});
My View Model:
private String API_KEY = "";
// This is observed from fragment
public MutableLiveData<SearchGifs> gifList = new MutableLiveData<>();
public HomeViewModel(String api_key) {
this.API_KEY = api_key;
}
GifRepo gifRepo = new GifRepo();
public void queryTextChange(String text, String key){
//Calls the repo and updates the mutablelivedata which is observed from fragment
gifList.postValue(gifRepo.queryTextChange(text,key).getValue());
}
Repository :
public MutableLiveData<SearchGifs> queryTextChange(String text, String api_key) {
MutableLiveData<SearchGifs> list = new MutableLiveData<>();
Call<SearchGifs> gifSearch = service.search(api_key,text);
gifSearch.enqueue(new Callback<SearchGifs>() {
@Override
public void onResponse(Call<SearchGifs> call, Response<SearchGifs> response) {
//getting data here perfectly
list.postValue(response.body());
}
@Override
public void onFailure(Call<SearchGifs> call, Throwable t) {
}
});
//doesn't wait for api call and returning null
return list;
}
Fragment: (Where it should update the recyclerview)
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Not Getting triggered
model.gifList.observe(requireActivity(), new Observer<SearchGifs>() {
@Override
public void onChanged(SearchGifs gifs) {
//RecyclerViewData will be updated here
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论