视图模型不断创建实时数据的实例 [英] View Model keeps creating instance of Live Data
问题描述
我在活动的 onCreate
方法中创建了 View Model 的实例.
I created the instance of View Model in onCreate
method of an activity.
ticketViewModel = ViewModelProviders.of(this).get(TicketViewModel.class);
然后我有一个方法,AddTicket
,它使用 viewModel
来点击服务,并在来自 viewModel
的响应中关闭加载动画.
Then i have a method, AddTicket
, which uses viewModel
to hit a service and on response from viewModel
i dismiss loading animation.
public void addTicket(View view){
ticketViewModel.AddTicket(id).observe(this, response ->{
dismissLoadingAnimation();
}
现在添加工单后,用户可以再次按下添加工单
按钮,addTicket()
方法将再次被调用.
Now after adding a ticket, user can repress the Add Ticket
button, and the addTicket()
method will be called again.
但是这次在 ViewModel 中定义的 observer
被调用了 2 次,导致 2 次网络调用和 2 次 dismissLoadingAnimation
执行.
but this time observer
defined in ViewModel gets called 2 times, resulting in 2 network calls, and 2 dismissLoadingAnimation
execution.
如果我一直按 addTicket
按钮,ViewModel
中定义的执行观察者的数量会增加.
And if i keep pressing addTicket
button, the number of executing observer defined inside ViewModel
keep increases.
这是我的视图模型代码.
public class TicketViewModel extends AndroidViewModel implements IServiceResponse {
MutableLiveData<String> mObservableResponse = new MutableLiveData<String>();
public MutableLiveData AddTicket(String id){
JsonObject jsonObject= new JsonObject();
jsonObject.addProperty("id", id);
NetworkUtility networkUtility= new NetworkUtility(this, ADD_TICKET);
networkUtility.hitService(URL, jsonObject, RequestMethods.POST);
return mObservableResponse;
}
@Override
public void onServiceResponse(String response, String callType){
if(serviceTag.equalsIgnoreCase(ADD_TICKET)){
mObservableResponse.setValue("success");
}
}
}
推荐答案
- Viewmodel 的目的是公开 observables (Livedata)
- View(Activity/Fragment) 的目的是获取这些可观察对象并观察它们
- 每当这些 observables(Livedata) 发生变化时,该变化就会自动发布到活动订阅的所有者 (Activity/Fragment),因此您无需在 onPause/onStop 中删除它们,因为它不是强制性的
我可以建议对您的代码进行一些更改以解决上述指针的问题
I can suggest few changes to your code to solve the problem with the above mentioned pointers
视图模型
public class TicketViewModel extends AndroidViewModel implements IServiceResponse {
MutableLiveData<String> mObservableResponse = new MutableLiveData<String>();
public LiveData<String> getResponseLiveData(){
return mObservableResponse;
}
public void AddTicket(String id){
JsonObject jsonObject= new JsonObject();
jsonObject.addProperty("id", id);
NetworkUtility networkUtility= new NetworkUtility(this, ADD_TICKET);
networkUtility.hitService(URL, jsonObject, RequestMethods.POST);
}
@Override
public void onServiceResponse(String response, String callType){
if(serviceTag.equalsIgnoreCase(ADD_TICKET)){
mObservableResponse.setValue("success");
}
}
}
查看
onCreate(){
ticketViewModel = ViewModelProviders.of(this).get(TicketViewModel.class);
observeForResponse();
}
private void observeForResponse(){
ticketViewModel.getResponseLiveData().observe(this, response ->{
//do what has to be updated in UI
}
}
public void addTicket(View view){
ticketViewModel.AddTicket(id);
}
希望这对您有所帮助:)
Hope this is of help :)
这篇关于视图模型不断创建实时数据的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!