Android + RxJava + For Loop + 未执行所有请求

huangapple go评论118阅读模式
英文:

Android + RxJava + For Loop + Not executing all the requests

问题

调用方法,

  1. for (String name : controllerToPartitionModels.keySet()) {
  2. List<PartitionModel> partitionsList = controllerToPartitionModels.get(name);
  3. refreshPartition(partitionsList, false);
  4. }

方法

  1. private void refreshPartition(List<PartitionModel> partitionModels, boolean isSyncAll) {
  2. ITModule.getITService()
  3. .refreshPartitionStatus(new ArrayList<>(partitionModels), isSyncAll)
  4. .subscribeOn(Schedulers.io())
  5. .observeOn(Schedulers.io())
  6. .subscribe(new Action() {
  7. @Override
  8. public void run() throws Exception {
  9. Logger.get().d(ATTActionManager.this, "Refreshing request sent successfully for list of size : " + partitionModels.size());
  10. }
  11. }, (@NonNull Throwable throwable) -> {
  12. Logger.get().d(ATTActionManager.this, "Error on Refresh request");
  13. });
  14. }

问题
如果有2个请求需要发送,有时我只会看到一个请求被发送。也就是说,即使for循环执行了两次以进行2次请求(HTTP),我只会看到一个请求被发送到服务器。
我在这里做错了什么?
RxJava 版本使用:2.2.19

英文:

Caller Of the method,

  1. for (String name : controllerToPartitionModels.keySet())
  2. {
  3. List&lt;PartitionModel&gt; partitionsList = controllerToPartitionModels.get(name);
  4. refreshPartition(partitionsList,false);
  5. }

Method

  1. private void refreshPartition(List&lt;PartitionModel&gt; partitionModels, boolean isSyncAll) {
  2. ITModule.getITService()
  3. .refreshPartitionStatus(new ArrayList&lt;&gt;(partitionModels), isSyncAll)
  4. .subscribeOn(Schedulers.io())
  5. .observeOn(Schedulers.io())
  6. .subscribe(new Action() {
  7. @Override
  8. public void run() throws Exception {
  9. Logger.get().d(ATTActionManager.this, &quot;Refreshing request sent successfully for list of size : &quot; + partitionModels.size());
  10. }
  11. }, (@NonNull Throwable throwable) -&gt; {
  12. Logger.get().d(ATTActionManager.this, &quot;Error on Refresh request&quot;);
  13. });
  14. }

Problem
If there are 2 requests that has to be sent, I sometime see only one request being sent. Meaning, even though for loop is executing twice for 2 request(HTTP), I see only one request is being sent to the server.
What is that i am doing wrong here?
Rxjava version in use : 2.2.19

答案1

得分: 2

你可以使用 flatMapIterable 合并上述的两种方法来解决你的问题。

合并后的解决方案:

  1. private void refreshPartition(Map<String, ?> controllerToPartitionModels) {
  2. Observable.just(controllerToPartitionModels)
  3. .map(controllerToPartitionModels -> controllerToPartitionModels.keySet())
  4. .flatMapIterable((Function<Set<String>, Iterable<String>>) name -> name)
  5. .map(name -> {
  6. boolean isSyncAll = false; // 你可以根据需求进行自定义
  7. return new Pair<List<PartitionModel>, Boolean>(controllerToPartitionModels.get(name), isSyncAll);
  8. })
  9. .flatMap((Function<Pair<List<PartitionModel>, Boolean>, ObservableSource<?>>) pair -> {
  10. boolean isSyncAll = pair.second;
  11. List<PartitionModel> partitionModels = pair.first;
  12. return ITModule.getITService()
  13. .refreshPartitionStatus(new ArrayList<>(partitionModels), isSyncAll);
  14. }
  15. )
  16. .subscribeOn(Schedulers.io())
  17. .observeOn(Schedulers.io())
  18. .subscribe(() -> {
  19. Logger.get().d(ATTActionManager.this, "刷新请求已成功发送,列表大小为: " + partitionModels.size());
  20. }, (@NonNull Throwable throwable) -> {
  21. Logger.get().d(ATTActionManager.this, "刷新请求出错");
  22. });
  23. }

*请将 ? 替换为有效的对象类型。

英文:

You can merge the above 2 methods to solve your problem by using flatMapIterable.

Merged Solution:

  1. private void refreshPartition(Map&lt;String, ?&gt; controllerToPartitionModels) {
  2. Observable.just(controllerToPartitionModels)
  3. .map(controllerToPartitionModels -&gt; controllerToPartitionModels.keySet())
  4. .flatMapIterable((Function&lt;Set&lt;String&gt;, Iterable&lt;String&gt;&gt;) name -&gt; name)
  5. .map(name -&gt; {
  6. boolean isSyncAll = false; // You can customise as per requirement
  7. return new Pair&lt;List&lt;PartitionModel&gt;, Boolean&gt;(controllerToPartitionModels.get(name), isSyncAll)
  8. })
  9. .flatMap((Function&lt;Pair&lt;List&lt;PartitionModel&gt;, Boolean&gt;, ObservableSource&lt;?&gt;&gt;) pair -&gt; {
  10. boolean isSyncAll = pair.first;
  11. List&lt;PartitionModel&gt; partitionModels = pair.second;
  12. return ITModule.getITService()
  13. .refreshPartitionStatus(new ArrayList&lt;&gt;(partitionModels), isSyncAll)
  14. }
  15. )
  16. .subscribeOn(Schedulers.io())
  17. .observeOn(Schedulers.io())
  18. .subscribe(new Action() {
  19. @Override
  20. public void run() throws Exception {
  21. Logger.get().d(ATTActionManager.this, &quot;Refreshing request sent successfully for list of size : &quot; + partitionModels.size());
  22. }
  23. }, (@NonNull Throwable throwable) -&gt; {
  24. Logger.get().d(ATTActionManager.this, &quot;Error on Refresh request&quot;);
  25. });
  26. }

*Kindly replace ? with the valid object type.

huangapple
  • 本文由 发表于 2020年8月3日 21:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63230087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定