我有多个请求(上传文件)到Observable
中,我想并行执行它们。代码是:
private void myMethod(List<String> filePathsList) {
List<Observable<String>> observables = new ArrayList<>();
for (String filePath : filePathsList) {
MultipartBody.Part multipartFile = getMultipartFile("some_file_name", filePath);
//here I'm just creating request from Retrofit restclient - the problem can't be here ;)
Observable<String> fileUploadObservable = UploadsRestClient.get().sendFile(multipartFile, "another_post_param");
observables.add(fileUploadObservable);
}
Observable<String> combinedObservable = Observable.zip(observables, new FuncN<String>() {
@Override
public String call(Object... args) {
return null;
}
});
combinedObservable.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
//called at the end
}
@Override
public void onError(Throwable throwable) {
//called if error occurs
}
@Override
public void onNext(String string) {
//should be called foreach request, but it's called only after the last one
}
});
}
问题在于,onNext()
仅在最后一次调用完成后才被调用,在onCompleted()
之前,如何在每次请求后触发?