android - Chain Single independent requests with RxJava
问题描述
I have a method to add an item to my account:
fun addItem(@Body addExperience: AddItem): Single<Item>
at some point the user can select multiple items and then add them all in bulk to his account. I want to chain these independent calls with RxJava2 (possibly in parallel) and then use the result of each to increment a progress bar.
Problem is --- i have no idea how to do it! Would this possibly be a Flowable? how can I generate this flowable from the multiple singles?
解决方案
我建议使用Flowable.fromIterable()
和flatMapSingle()
每个请求。您可以通知用户有关进度,因为在每个请求之后都会调用subscribe
onNext。
List<Single<Integer>> list = new ArrayList<>();
list.add(Single.just(1));
list.add(Single.just(2));
list.add(Single.just(3));
list.add(Single.just(4));
Flowable.fromIterable(list)
.flatMapSingle(singleRetrofitRequest -> singleRetrofitRequest, false, 2)
.subscribe(item -> System.out.println(item));
如您所见,我添加了额外的参数来flatMapSingle()
表示可能的并行请求数。
推荐阅读
- ios - 无法将 tableHeaderView 高度设置为与表格视图框架高度相同
- javascript - React.Js Book UI npm
- python - 在python中多次迭代txt文件的特定行
- c# - 取消 Parallel.foreach 中的后台运行作业
- javascript - 我无法在反应中对嵌套对象执行映射功能
- javascript - 定义两种返回类型时,属性“x”不存在
- python - 如何生成最有可能在给定句子中占据缺失标记位置的标记列表?
- ansible - Ansible如何找到inventory、group_vars、all.yaml、user_vars的位置
- javascript - 未捕获(承诺中)类型错误:无法读取 null 的属性“长度”
- facebook - Facebook Graph API webhook 未在 DM 中发送 GIF 图像附件