我试图在调用n次休息服务后处理结果,但是一个属性告诉我是否要调用Web服务的对象列表,但是在完成n次调用时我无法处理最终结果,它似乎这是由于异步,有人可以告诉我我做错了什么吗?非常感谢,并原谅我糟糕的英语。
示例代码是:
private List<ResquestService> request;
private void process(){
Observable.fromIterable(request)
.filter(resquestService -> resquestService.getType().equals(WS_TYPE))
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(
resquestService -> {consumeRestService(resquestService);},
Throwable::printStackTrace,
() -> {processFinalResults();}
);
}
private void consumeRestService(ResquestService resquestService){
getApi().processData(resquestService)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.subscribe(
next -> {processResultWS();},
error -> {handleErrorWS();}
);
}
public class ResquestService {
private int id;
private String name;
private String type;
}
解决方案是放入可迭代的.subscribeOn(Schedulers.newThread())
并在对休息服务的调用中:.observeOn(AndroidSchedulers.mainThread())
private void process(){
Observable.fromIterable(request)
.filter(resquestService -> resquestService.getType().equals(WS_TYPE))
.subscribeOn(Schedulers.newThread())
.subscribe(
resquestService -> {consumeRestService(resquestService);},
Throwable::printStackTrace,
() -> {processFinalResults();}
);
}
private void consumeRestService(ResquestService resquestService){
getApi().processData(resquestService)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
next -> {processResultWS();},
error -> {handleErrorWS();}
);
}