3 回答
TA贡献1811条经验 获得超5个赞
在你的情况下,你可以toList()这样申请
fun doTask(items: List<String>):Observable<Boolean>{
return Observable.fromIterable(items)
.flatMap { processItem(it) }
.toList()
.toObservable()
.flatMap { finalTask() }
}
TA贡献1998条经验 获得超6个赞
这个问题可以用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 }
}
TA贡献1795条经验 获得超7个赞
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
}
});
添加回答
举报