要求执行api请求并下载作为响应返回的所有资产。我有以下逻辑,但不确定执行此操作的正确方法是什么。 public Observable<Response> prepareSomething() { return api.requestSomthing() .observeOn(Schedulers.io()) .flatMap(response -> Observable.fromIterable(response.getAssetsList())) .map(asset -> downloadAsset(asset, response.getSomeValue/*how?*/)) .someRxOperatorToRetrunResponseObject(/*how?*/);}我实际上找到了一种通过以下方式实现它的方法:public Observable<Response> prepareSomething() { return api.requestSomthing() .observeOn(Schedulers.io()) .flatMap(response -> { ArrayList<Observable<Asset>> observables = new ArrayList<>(); for(Asset asset : response.getAssetsList()){ Observable downloadOb = Observable.just(asset) .map(assetToDownload-> downloadAsset(assetToDownload, response.getSomeValue())); observables.add(downloadOb); } return Observable.zip(observables, objects -> response); });}但这看起来不太好。赞赏是否有人可以帮助证明这一点。
添加回答
举报
0/150
提交
取消