我曾尝试使用“ Observable.create”来获取异步回调,但可观察对象并不等待发出的每个项目在 emitter.onNext 中完成下载。
getListA()
.flatMap(aList -> {
int total = aList.size();
return Observable.fromIterable(aList)
.flatMap(a -> Observable.create(emitter -> {
// ----------------------------------------------
AsyncDownloader
.download(remoteUrl, filePath, fileName)
.build()
.setOnStartOrResumeListener(() -> {
Timber.d("Download Start Thread ==> %s", Thread.currentThread());
})
.setOnProgressListener(progress -> {
int progressPercent = (int)
(progress.currentBytes * 100 / progress.totalBytes);
mView.onDownloadProgress(
progressPercent, progress.currentBytes,
progress.totalBytes, fileName,
total, currentFileCount);
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
emitter.onNext(a);
emitter.onComplete();
currentFileCount++;
}
@Override
public void onError(Error error) {
}
});
//-----------------------------------------------------
}));
.toList()
.toObservable();
})
问题在于,对于 Observable.fromIterable()发出的每个A,都不会调用 emitter.onNext 来等待完成。项目会在 AsyncDownloader 完成之前发出。然后调用 emitter.OnNext !
**我想完成下载,然后从列表中调用“下一项”。**
..方法是否错误..请帮助!