android - 如何等到一堆电话结束再拨打另一个电话
问题描述
我正在使用RxJava
并且我知道concat
,我想它确实适合我,因为我想先完成所有第一次通话,然后再进行第二次通话,但我不知道如何实现它。
从现在开始我有这个:
private fun assignAllAnswersToQuestion(questionId: Long) {
answerListCreated.forEach { assignAnswerToQuestion(questionId, it.id) }
}
private fun assignAnswerToQuestion(questionId: Long, answerId: Long) {
disposable = questionService.addAnswerToQuestion(questionId,answerId,MyUtils.getAccessTokenFromLocalStorage(context = this))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
result -> //Do nothing it should call the next one
},
{ error -> toast(error.message.toString())}
)
}
但是,一旦这一切完成,forEach
我想做这样的事情:
private fun assignAllAnswersToQuestion(questionId: Long) {
answerListCreated.forEach { assignAnswerToQuestion(questionId, it.id)
anotherCallHere(questionId) //Do it when the first forEach is finished!!
}
任何想法?
另外,这是一种使用协程的方法吗?
解决方案
我想你得把.map
你的列表( answerListCreated
) 放到一个列表中Flowable
,然后Flowable.zip
在这个列表上使用。
zip
用于将Flowable
s 的结果组合成一个结果。由于您不需要这些结果,我们将忽略它们。
在zip
您确定所有之前Flowable
的 s 都结束后,您可以.flatMap
执行下一个调用(假设anotherCallHere
返回一个Flowable
.
最后,它将是这样的:
val flowableList = answerListCreated.map { assignAnswerToQuestion(questionId, it.id) }
disposable = Flowable.zip(flowableList) { /* Ignoring results */ }
.flatMap { anotherCallHere(questionId) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
// ...
}
需要注意的是,如果任何一个调用失败,整个链都会失败(onError
将被调用)。
推荐阅读
- reactjs - Jest Enzyme 测试用例覆盖率
- ios - 无法在 branch.io 上查看自定义事件
- flutter - Flutter 中的偏移与对齐
- python - 如何将元素删除到 mxnet NDArray?
- mongodb - 仅当该字段存在于该文档中时才需要过滤该字段上的 mongo 文档
- java - Java 中具有自定义过期时间的事件流的持久缓存
- angular - 离开输入控件时如何触发 *ngIf 指令进行验证
- dart - StreamBuilder 抛出 Dirty State 说 Invalid Arguments
- jhipster - 生成后如何禁用 Jhipster 本地存储库自动提交功能?
- powershell - 通过powershell脚本重新排序章鱼步骤