我正在尝试使用 OkHttp 下载文件并使用 Okio 写入磁盘。我还为此过程创建了一个 rx observable。它正在工作,但是它比我以前使用的(Koush 的 Ion 库)明显慢。
以下是我创建可观察对象的方法:
public Observable<FilesWrapper> download(List<Thing> things) {
return Observable.from(things)
.map(thing -> {
File file = new File(getExternalCacheDir() + File.separator + thing.getName());
if (!file.exists()) {
Request request = new Request.Builder().url(thing.getUrl()).build();
Response response;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful()) new IOException();
else {
BufferedSink sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.close();
}
} catch (IOException e) {
new IOException();
}
}
return file;
})
.toList()
.map(files -> new FilesWrapper(files);
}
有谁知道什么可能导致速度慢,或者我是否错误地使用了运算符?
使用 flatMap 而不是 map 将允许您并行执行下载:
public Observable<FilesWrapper> download(List<Thing> things) {
return Observable.from(things)
.flatMap(thing -> {
File file = new File(getExternalCacheDir() + File.separator + thing.getName());
if (file.exists()) {
return Observable.just(file);
}
final Observable<File> fileObservable = Observable.create(sub -> {
if (sub.isUnsubscribed()) {
return;
}
Request request = new Request.Builder().url(thing.getUrl()).build();
Response response;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful()) { throw new IOException(); }
} catch (IOException io) {
throw OnErrorThrowable.from(OnErrorThrowable.addValueAsLastCause(io, thing));
}
if (!sub.isUnsubscribed()) {
try (BufferedSink sink = Okio.buffer(Okio.sink(file))) {
sink.writeAll(response.body().source());
} catch (IOException io) {
throw OnErrorThrowable.from(OnErrorThrowable.addValueAsLastCause(io, thing));
}
sub.onNext(file);
sub.onCompleted();
}
});
return fileObservable.subscribeOn(Schedulers.io());
}, 5)
.toList()
.map(files -> new FilesWrapper(files));
}
我们使用 flatMap 上的 maxConcurrent 限制每个订阅者同时请求的数量。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)