Search code examples
javaandroidperformancerx-java

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


Caller Of the method,

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

Method

 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");
            });
}

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


Solution

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

    Merged Solution:

    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; // You can customise as per requirement
                    return new Pair<List<PartitionModel>, Boolean>(controllerToPartitionModels.get(name), isSyncAll)
                })
                .flatMap((Function<Pair<List<PartitionModel>, Boolean>, ObservableSource<?>>) pair -> {
                            boolean isSyncAll = pair.first;
                            List<PartitionModel> partitionModels = pair.second;
                            return 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");
                });
    
    }
    

    *Kindly replace ? with the valid object type.