title: RxJava for each item in a list make api request with Retrofit in Android
body: <p>I have the following issue in my Android project:</p>
  
  <p>I have a list of elements (ArrayList) obtained from a previous network call using Retrofit. 
  CategoryItem looks like this:</p>
  
  <pre><code>public class CategoryItem {
     String id;
     String name;
     public CategoryItem(String id, String name) {
       this.id = id;
       this.name = name;
     }
     public String getId() {
       return id;
     }
     public String getName() {
       return name;
     }
  }
  </code></pre>
  
  <p>Practically the categories is made of 20 elements. Now I need to make a network API and obtain the list of products for each of the categories. In order to do so, the products API has the id from the category as query parameter. After I obtain the list of products of a certain categoy I am adding it into internal SQLite DB.</p>
  
  <p>I would like to do this with RxJava (either 1 and 2).</p>
  
  <p>What I have done up to now is not multi-threaded and is on the MainThread which is not correct. 
  I will add the code snippet here:</p>
  
  <pre><code>for (int i = 0; i &lt; categoryItems.size(); i++) {
      CategoryItem categoryItem = categoryItems.get(i);
      final String iCat = categoryItem.getId();
      Observable&lt;ProductResponse&gt; call = networkManager.getApiServiceProductsRx().getProductsRx(iCat);
  
      Subscription subscription = call
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Subscriber&lt;ProductResponse&gt;() {
                  @Override
                  public void onCompleted() {
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      if (e instanceof HttpException) {
                          HttpException response = (HttpException) e;
                          int code = response.code();
                          Log.d("RetrofitTest", "Error code: " + code);
                      }
                  }
  
                  @Override
                  public void onNext(ProductResponse response) {
                      mProductItemsBelongingToACategory = new ArrayList&lt;&gt;();
                      mProductItemsBelongingToACategory = response.getProductChannel().getProductItems();
                      mDatabaseHandler.addProducts(iCat, mProductItemsBelongingToACategory);
                  }
              });
  
      subscriptions2.add(subscription);
  
  }
  </code></pre>
  
  <p>I would like to do this with flatMap or flatMapIterable and eventually using multi-threading.</p>