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

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

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

问题

调用方法,

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

方法

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

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

英文:

Caller Of the method,

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

Method

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

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 合并上述的两种方法来解决你的问题。

合并后的解决方案:

private void refreshPartition(Map<String, ?> controllerToPartitionModels) {

    Observable.just(controllerToPartitionModels)
            .map(controllerToPartitionModels -> controllerToPartitionModels.keySet())
            .flatMapIterable((Function<Set<String>, Iterable<String>>) name -> name)
            .map(name -> {
                boolean isSyncAll = false; // 你可以根据需求进行自定义
                return new Pair<List<PartitionModel>, Boolean>(controllerToPartitionModels.get(name), isSyncAll);
            })
            .flatMap((Function<Pair<List<PartitionModel>, Boolean>, ObservableSource<?>>) pair -> {
                        boolean isSyncAll = pair.second;
                        List<PartitionModel> partitionModels = pair.first;
                        return ITModule.getITService()
                                .refreshPartitionStatus(new ArrayList<>(partitionModels), isSyncAll);
                    }
            )
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .subscribe(() -> {
                Logger.get().d(ATTActionManager.this, "刷新请求已成功发送,列表大小为: " + partitionModels.size());
            }, (@NonNull Throwable throwable) -> {
                Logger.get().d(ATTActionManager.this, "刷新请求出错");
            });

}

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

英文:

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

Merged Solution:

private void refreshPartition(Map&lt;String, ?&gt; controllerToPartitionModels) {

    Observable.just(controllerToPartitionModels)
            .map(controllerToPartitionModels -&gt; controllerToPartitionModels.keySet())
            .flatMapIterable((Function&lt;Set&lt;String&gt;, Iterable&lt;String&gt;&gt;) name -&gt; name)
            .map(name -&gt; {
                boolean isSyncAll = false; // You can customise as per requirement
                return new Pair&lt;List&lt;PartitionModel&gt;, Boolean&gt;(controllerToPartitionModels.get(name), isSyncAll)
            })
            .flatMap((Function&lt;Pair&lt;List&lt;PartitionModel&gt;, Boolean&gt;, ObservableSource&lt;?&gt;&gt;) pair -&gt; {
                        boolean isSyncAll = pair.first;
                        List&lt;PartitionModel&gt; partitionModels = pair.second;
                        return ITModule.getITService()
                                .refreshPartitionStatus(new ArrayList&lt;&gt;(partitionModels), isSyncAll)
                    }
            )
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.io())
            .subscribe(new Action() {
                @Override
                public void run() throws Exception {
                    Logger.get().d(ATTActionManager.this, &quot;Refreshing request sent successfully for list of size : &quot; + partitionModels.size());
                }
            }, (@NonNull Throwable throwable) -&gt; {
                Logger.get().d(ATTActionManager.this, &quot;Error on Refresh request&quot;);
            });

}

*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:

确定