不幸的是,我的REST Delete操作仅适用于一项。所以我想做的是,
Observable.just(items).flatMapIterable { items -> items }.flatMap {
//call REST DELETE for every item
}.flatMap {
// call REST GET
}
问题是每个项目都被调用GET。如何等待完成所有删除操作,然后执行GET调用?
谢谢。
答案 0 :(得分:1)
根据您的情况,您可以像这样申请toList()
fun doTask(items: List<String>):Observable<Boolean>{
return Observable.fromIterable(items)
.flatMap { processItem(it) }
.toList()
.toObservable()
.flatMap { finalTask() }
}
答案 1 :(得分:0)
可以使用zip解决问题。如果有人想要这个
fun doTask(items: ArrayList<String>): Observable<Boolean> {
val list = arrayListOf<Observable<String>>()
items.forEach {
list.add(processItem(it))
}
return Observable.zip(list) {
}.flatMap {
finalTask()
}
}
fun processItem(s: String): Observable<String> {
print(s)
return Observable.just(s.toUpperCase())
}
fun finalTask(): Observable<Boolean> {
print("final")
return Observable.fromCallable { true }
}
答案 2 :(得分:0)
Observable.just("one", "two", "three", "four", "five").subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.d("ffff", s);//print: one, two, three, four, five
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
Log.d("ffff", "complete");//print: complete
}
});