RxJava - fetch every item on the list and emits one-by-one (any order)












0















I have a method that returns an Observable<List<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>. currently I'm doing it by below code.



    @Override
public Observable<List<Item>> getResponses(List<Long> requests) {
return Observable.from(requests).
flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
}


It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext() get invoked,



Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext should be invoked every-time time individually for each item.



How would I do this using RxJava operators?










share|improve this question





























    0















    I have a method that returns an Observable<List<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>. currently I'm doing it by below code.



        @Override
    public Observable<List<Item>> getResponses(List<Long> requests) {
    return Observable.from(requests).
    flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
    }


    It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext() get invoked,



    Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext should be invoked every-time time individually for each item.



    How would I do this using RxJava operators?










    share|improve this question



























      0












      0








      0








      I have a method that returns an Observable<List<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>. currently I'm doing it by below code.



          @Override
      public Observable<List<Item>> getResponses(List<Long> requests) {
      return Observable.from(requests).
      flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
      }


      It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext() get invoked,



      Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext should be invoked every-time time individually for each item.



      How would I do this using RxJava operators?










      share|improve this question
















      I have a method that returns an Observable<List<Long>>, which are ids of some Items. I'd like to go through this list and download every Item using another method that returns Observable<Item>. currently I'm doing it by below code.



          @Override
      public Observable<List<Item>> getResponses(List<Long> requests) {
      return Observable.from(requests).
      flatMap((Func1<Long, Observable<Item>>) restRequest -> getResponseEach(restRequest)).toList();
      }


      It's working fine, but it returning all the response in on go, I mean when all download get finish then my onNext() get invoked,



      Main Question But alternatively I need to emit every response one-by-one(Any Order) once each item fetched successfully from server, so my onNext should be invoked every-time time individually for each item.



      How would I do this using RxJava operators?







      android rx-java rx-java2 rx-android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 5:26









      Sarath Kn

      1,613919




      1,613919










      asked Nov 21 '18 at 4:52









      Lavekush AgrawalLavekush Agrawal

      4,36563773




      4,36563773
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You have to remove the toList() operator. The toList() will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>



          You can return the observable returned by the flatMap in your code and you will get the results one by one,



          public Observable<Item> getResponses(List<Long> requests) {
          return Observable.fromIterable(requests)
          .flatMap(restRequest -> getResponseEach(restRequest));
          }


          Now, your getResponses method will return Observable<Item> instead of Observable<List<Item>>



          And you can subscribe to this function as follows



              getResponses(ids)
          .subscribe(new DisposableObserver<Item>() {
          @Override
          public void onNext(Item item) {
          // each item will be received here one by one
          }

          @Override
          public void onError(Throwable e) {
          // handle any occured error during the operation
          }

          @Override
          public void onComplete() {
          // all operations completed
          }
          });





          share|improve this answer


























          • Already tried seems not working for me.

            – Lavekush Agrawal
            Nov 21 '18 at 5:54











          • Pls post your full code. Till the subscription

            – Sarath Kn
            Nov 21 '18 at 5:55











          • your getResponseEach returns Observable<Item> right?

            – Sarath Kn
            Nov 21 '18 at 5:59













          • yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

            – Lavekush Agrawal
            Nov 21 '18 at 6:28











          • You are using Rxjava 1 or 2

            – Sarath Kn
            Nov 21 '18 at 6:37











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405454%2frxjava-fetch-every-item-on-the-list-and-emits-one-by-one-any-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You have to remove the toList() operator. The toList() will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>



          You can return the observable returned by the flatMap in your code and you will get the results one by one,



          public Observable<Item> getResponses(List<Long> requests) {
          return Observable.fromIterable(requests)
          .flatMap(restRequest -> getResponseEach(restRequest));
          }


          Now, your getResponses method will return Observable<Item> instead of Observable<List<Item>>



          And you can subscribe to this function as follows



              getResponses(ids)
          .subscribe(new DisposableObserver<Item>() {
          @Override
          public void onNext(Item item) {
          // each item will be received here one by one
          }

          @Override
          public void onError(Throwable e) {
          // handle any occured error during the operation
          }

          @Override
          public void onComplete() {
          // all operations completed
          }
          });





          share|improve this answer


























          • Already tried seems not working for me.

            – Lavekush Agrawal
            Nov 21 '18 at 5:54











          • Pls post your full code. Till the subscription

            – Sarath Kn
            Nov 21 '18 at 5:55











          • your getResponseEach returns Observable<Item> right?

            – Sarath Kn
            Nov 21 '18 at 5:59













          • yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

            – Lavekush Agrawal
            Nov 21 '18 at 6:28











          • You are using Rxjava 1 or 2

            – Sarath Kn
            Nov 21 '18 at 6:37
















          1














          You have to remove the toList() operator. The toList() will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>



          You can return the observable returned by the flatMap in your code and you will get the results one by one,



          public Observable<Item> getResponses(List<Long> requests) {
          return Observable.fromIterable(requests)
          .flatMap(restRequest -> getResponseEach(restRequest));
          }


          Now, your getResponses method will return Observable<Item> instead of Observable<List<Item>>



          And you can subscribe to this function as follows



              getResponses(ids)
          .subscribe(new DisposableObserver<Item>() {
          @Override
          public void onNext(Item item) {
          // each item will be received here one by one
          }

          @Override
          public void onError(Throwable e) {
          // handle any occured error during the operation
          }

          @Override
          public void onComplete() {
          // all operations completed
          }
          });





          share|improve this answer


























          • Already tried seems not working for me.

            – Lavekush Agrawal
            Nov 21 '18 at 5:54











          • Pls post your full code. Till the subscription

            – Sarath Kn
            Nov 21 '18 at 5:55











          • your getResponseEach returns Observable<Item> right?

            – Sarath Kn
            Nov 21 '18 at 5:59













          • yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

            – Lavekush Agrawal
            Nov 21 '18 at 6:28











          • You are using Rxjava 1 or 2

            – Sarath Kn
            Nov 21 '18 at 6:37














          1












          1








          1







          You have to remove the toList() operator. The toList() will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>



          You can return the observable returned by the flatMap in your code and you will get the results one by one,



          public Observable<Item> getResponses(List<Long> requests) {
          return Observable.fromIterable(requests)
          .flatMap(restRequest -> getResponseEach(restRequest));
          }


          Now, your getResponses method will return Observable<Item> instead of Observable<List<Item>>



          And you can subscribe to this function as follows



              getResponses(ids)
          .subscribe(new DisposableObserver<Item>() {
          @Override
          public void onNext(Item item) {
          // each item will be received here one by one
          }

          @Override
          public void onError(Throwable e) {
          // handle any occured error during the operation
          }

          @Override
          public void onComplete() {
          // all operations completed
          }
          });





          share|improve this answer















          You have to remove the toList() operator. The toList() will emit only after all the emissions of the upstream have been completed, and it will collect the results and will emit as a Single<List<YourResultObject>>



          You can return the observable returned by the flatMap in your code and you will get the results one by one,



          public Observable<Item> getResponses(List<Long> requests) {
          return Observable.fromIterable(requests)
          .flatMap(restRequest -> getResponseEach(restRequest));
          }


          Now, your getResponses method will return Observable<Item> instead of Observable<List<Item>>



          And you can subscribe to this function as follows



              getResponses(ids)
          .subscribe(new DisposableObserver<Item>() {
          @Override
          public void onNext(Item item) {
          // each item will be received here one by one
          }

          @Override
          public void onError(Throwable e) {
          // handle any occured error during the operation
          }

          @Override
          public void onComplete() {
          // all operations completed
          }
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 5:37

























          answered Nov 21 '18 at 5:26









          Sarath KnSarath Kn

          1,613919




          1,613919













          • Already tried seems not working for me.

            – Lavekush Agrawal
            Nov 21 '18 at 5:54











          • Pls post your full code. Till the subscription

            – Sarath Kn
            Nov 21 '18 at 5:55











          • your getResponseEach returns Observable<Item> right?

            – Sarath Kn
            Nov 21 '18 at 5:59













          • yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

            – Lavekush Agrawal
            Nov 21 '18 at 6:28











          • You are using Rxjava 1 or 2

            – Sarath Kn
            Nov 21 '18 at 6:37



















          • Already tried seems not working for me.

            – Lavekush Agrawal
            Nov 21 '18 at 5:54











          • Pls post your full code. Till the subscription

            – Sarath Kn
            Nov 21 '18 at 5:55











          • your getResponseEach returns Observable<Item> right?

            – Sarath Kn
            Nov 21 '18 at 5:59













          • yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

            – Lavekush Agrawal
            Nov 21 '18 at 6:28











          • You are using Rxjava 1 or 2

            – Sarath Kn
            Nov 21 '18 at 6:37

















          Already tried seems not working for me.

          – Lavekush Agrawal
          Nov 21 '18 at 5:54





          Already tried seems not working for me.

          – Lavekush Agrawal
          Nov 21 '18 at 5:54













          Pls post your full code. Till the subscription

          – Sarath Kn
          Nov 21 '18 at 5:55





          Pls post your full code. Till the subscription

          – Sarath Kn
          Nov 21 '18 at 5:55













          your getResponseEach returns Observable<Item> right?

          – Sarath Kn
          Nov 21 '18 at 5:59







          your getResponseEach returns Observable<Item> right?

          – Sarath Kn
          Nov 21 '18 at 5:59















          yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

          – Lavekush Agrawal
          Nov 21 '18 at 6:28





          yes, but instead of Observable.fromIterable I'm using Observable.from(), just bcoz fromIterable in available.

          – Lavekush Agrawal
          Nov 21 '18 at 6:28













          You are using Rxjava 1 or 2

          – Sarath Kn
          Nov 21 '18 at 6:37





          You are using Rxjava 1 or 2

          – Sarath Kn
          Nov 21 '18 at 6:37


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405454%2frxjava-fetch-every-item-on-the-list-and-emits-one-by-one-any-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith