0

I'm sending multiple API requests(>1000 requests) using Observable.zip(), see the below implementation, here I want to wait until the last responses arrive, I read the documentation for RxJava, but seems like no such method for waiting until last responses for zip, is there API available for that? or is there any way to implement such functionality? Thanks in Advance,

         Observable.zip(
                        requests, // list of requests
                        new Function<Object[], List<Object>>() {
                            @Override
                            public List<Object> apply(Object[] objects) throws Exception {
                                Log.d("onSubscribe", "apply: " + objects.length);
                                
                                for (Object o : objects) {
                                    messageResponse.add((Object) o);
                                }

                                return messageResponse;
                            }
                        }).subscribeOn(Schedulers.io())
                .subscribe(
                        new Consumer<List<Object>>() {
                            @Override
                            public void accept(List<Object> dataResponses) throws Exception {
                                Log.d("onSubscribe", "YOUR DATA IS HERE: " + dataResponses);
                               

                            }
                        },

                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable e) throws Exception {
                                Log.e("onSubscribe", "Throwable: " + e.getMessage());
                                e.getStackTrace();                               
                               
                            }
                        }
                );
FGH
  • 2,900
  • 6
  • 26
  • 59
  • 1
    What do you mean by last responses? `zip` runs the lambda when all sources have produced an item. If any of them fails, the sequence fails. If they are not the same length, you'll get as many lambda runs as the shortest sequence. – akarnokd Nov 02 '22 at 09:05
  • what I meant in the last response is, I need to see how many responses got succeeded and how many got failed, so then I can filter the failed responses and retry again – FGH Nov 02 '22 at 09:59
  • You can materialize the sources and distinguish between notifications with value and with error. Why not put the retries onto the sources themselves so no need to juggle with the failed sources? – akarnokd Nov 02 '22 at 10:52
  • I understood your point, but here thing is I want to show some animation in my application(background task) until I get the responses, so according to my use case so, if all the requests arrived, then I can stop it and show the success rate, and there is one option to stop the request while sending the messages, so it's necessary for my requirement – FGH Nov 02 '22 at 11:47
  • What you are describing is still `zip` with materialized sources. If all sources produce one notification, the zip's lambda will be invoked where you can stop the animation. – akarnokd Nov 02 '22 at 12:03

0 Answers0