等待获得布尔值为true
问题描述:
下面这段代码是一个for循环上的Web服务如何要等到布尔每次循环等待获得布尔值为true
检查代码
for (i=0;i<contactsString.length-1;i++){
Phone phone=new Phone();
phone.phone=contactsString[i];
check=false;
WebService.getInstance().getApi().checkNumber(phone).enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
availableUsers++;
check=true;
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
}
});
//--- here how to wait untill check is true then continue the loop
}
答
的评论怎么之前是真实的等待,直到检查真,则继续循环
使用递归函数
将字段初始化为availableUsers=0
。要启动循环,请致电checkNumber()
private int availableUsers=0;
public void checkNumber() {
this.checkNumber(0);
}
private void checkNumber(final int i){
if(contactsString==null || i>=contactsString.length){
return;
}
Phone phone=new Phone();
phone.phone=contactsString[i];
WebService.getInstance().getApi().checkNumber(phone).enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
availableUsers++;
checkNumber(i+1);
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
checkNumber(i+1);
}
});
}
您在哪里指定代码中的check = true。 –
内onResponse – RealDEV
在地方循环使用递归函数的.....这将很好地工作在这种情况下 – santoXme