title: How to iterate a Flowable from the result of a Single
body: <p>I am using Retrofit with Rxjava to retrieve a Single response.</p>
  
  <pre><code>@GET("/commonapi/search")
  Single&lt;MyEntity&gt; requestAddressRx(@Query("searchVal") String searchVal,
                                     @Query("pageNum") int pageNum);
  </code></pre>
  
  <p>I have a json that will return the following</p>
  
  <pre><code>{"found":240,"totalNumPages":16,"pageNum":1,"results":[...
  </code></pre>
  
  <p>Currently, to retrieve all 16 pages, I have to do the following: -</p>
  
  <ol>
  <li>call requestAddressRx() one time to get the 1st result for the totalNumPages, </li>
  <li>and then create a separate Flowable calling requestAddressRx() to loop from page 2 to 16.</li>
  </ol>
  
  <p>Is there a way where I can combine step 1 and 2 together?</p>
  
  <hr>
  
  <p>//After some bumbling around - what i currently have is </p>
  
  <pre><code>    Single&lt;List&lt;MyEntity&gt;&gt; result =  addressRetrofitRx.requestAddressQueryRx(query, 1)
              //now i get the number of pages from the results
              //  -NOTE: the result is now discarded :(
              .map( r-&gt; r.getTotalNumPages() ) 
              //now i create a flowable from the number of pages
              .flatMapPublisher(totalNumPages -&gt; Flowable.range(1, totalNumPages))
              .flatMapSingle(pageNumber -&gt; addressRetrofitRx.requestAddressQueryRx(query, pageNumber))
              .toList();
  </code></pre>
  
  <p>This causes Rxjava to call page 1 twice (the first time is read and discarded after I read the totalNumPages).</p>
  
  <p>Also if the result only have 1 totalNumPages, I will still spawn the flowable.</p>
  
  <p>There ought to be a better way to approach this problem, but I can't seem to figure it out. JSON schemas where the numberOfPages is encoded in the result are very common, so I assumed there's a correct RxJava way of parsing them.</p>