对列表中的所有项目执行一个任务,然后在 RxJava 中执行另一个任务

不幸的是,我的 REST 删除操作仅适用于一项。所以我想做的是,


Observable.just(items).flatMapIterable { items -> items }.flatMap {

                //call REST DELETE for every item

            }.flatMap {

                // call REST GET

            }

问题是正在为每个项目调用 GET 调用。我如何等待完成所有删除完成然后执行 GET 调用?


提前致谢。


白衣染霜花
浏览 160回答 3
3回答

四季花海

在你的情况下,你可以toList()这样申请fun doTask(items: List<String>):Observable<Boolean>{&nbsp; &nbsp; &nbsp; &nbsp; return Observable.fromIterable(items)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap { processItem(it) }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toObservable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap { finalTask() }&nbsp; &nbsp; }

米琪卡哇伊

这个问题可以用zip解决。如果有人想要这个fun doTask(items: ArrayList<String>): Observable<Boolean> {&nbsp; &nbsp; val list = arrayListOf<Observable<String>>()&nbsp; &nbsp; items.forEach {&nbsp; &nbsp; &nbsp; &nbsp; list.add(processItem(it))&nbsp; &nbsp; }&nbsp; &nbsp; return Observable.zip(list) {&nbsp; &nbsp; }.flatMap {&nbsp; &nbsp; &nbsp; &nbsp; finalTask()&nbsp; &nbsp; }}fun processItem(s: String): Observable<String> {&nbsp; &nbsp; print(s)&nbsp; &nbsp; return Observable.just(s.toUpperCase())}fun finalTask(): Observable<Boolean> {&nbsp; &nbsp; print("final")&nbsp; &nbsp; return Observable.fromCallable { true }}

一只萌萌小番薯

Observable.just("one", "two", "three", "four", "five").subscribe(new Consumer<String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void accept(String s) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("ffff", s);//print: one, two, three, four, five&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, new Consumer<Throwable>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void accept(Throwable throwable) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, new Action() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("ffff", "complete");//print: complete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java