在MainActivity中:
mPresenter = new MainPresenter();
public void initData(){
showLoadingDialog();
mPresenter.queryFood();
mPresenter.queryFruit();
}
在MainPresenter文件中:
/初始化并行请求数量
public int mRequestCount = 0;
public void increaseRequestCount(){
mRequestCount++;
}
public void decreaseRequestCount(){
mRequestCount--;
if(mRequestCount < 1){
closeLoadingDialog();
}
}
public void queryFood(){
increaseRequestCount();
postData(Constants.CODE_QUERY_FOOD);
}
public void queryFruit(){
increaseRequestCount();
postData(Constants.CODE_QUERY_FRUIT);
}
public void onSuccess(int requestCode){
if(requestCode ==Constants.CODE_QUERY_FOOD){
decreaseRequestCount();
}else if(requestCode ==Constants.CODE_QUERY_FRUIT)
decreaseRequestCount();
}
当所有操作都在主线程中,则按上述操作。
当操作涉及多个线程,则需要Synchronous同步操作,否则数据会计算错误。