我目前正在使用RxJava,并且我有类似的内容,
List<Integer> taskIds = ...
Observable.just(new Task(taskIds.get(0)))
.compose(executeWorkFlow())
.filter(Response::isSuccess)
.flatMap(res -> Observable.just(new Task(taskIds.get(1)))
.compose(executeWorkFlow())
.filter(Response::isSuccess)
.flatMap(res -> Observable.just(new Task(taskIds.get(2)))
.compose(executeWorkFlow())
.filter(Response::isSuccess)
...
execute()是执行API调用并返回响应(即
)的Transformer。ObservableTransformer<Task, Response> execute() { ... }
是否有更好的书写方式?鉴于我不知道从以前的Observable中会发出多少个任务ID。
答案 0 :(得分:1)
使用fromIterable
,concatMap
和takeUntil(Predicate)
:
Observable.fromIterable(tasks)
.concatMap(task ->
Observable.just(new Task(task))
.compose(executeWorkFlow())
)
.takeUntil(response -> !response.isSuccess)