title: Download list of files with retrofit and rxJava
body: <p>I´m trying to download a list of files from an api with retrofit and rxjava i read a lot but i can't figurated how retrofit and rxjava work together, and im not sure what kind of thread must use on the observer</p>
  
  <p>I´m trying changing the file download process of an application originally was made with doAsync (Anko), The application run in a very limited tablet with api 19 and slow internet connection. The inicial problema was thath the tablet expermient leak of memory in the first files download (about 200 files, using 100 mb of memory (all them)). So after a long research i decide to try rewrite the code for the download process using Rx and retrofit (but i nos have much experince in kotlin or movil). </p>
  
  <p>my Model</p>
  
  <pre><code>data class File(
           val id: String
  )      //staging
  
  data class Movimientos(val borrar: List&lt;File&gt;,
                         val descargar: List&lt;File&gt;,
                         val estructura: List&lt;Nodo&gt;)
  
  data class Nodo( val id: String,
                   val root: String,
                   val name: String,
                   val file: Boolean,
                   val descargado: Boolean,
                   val version: Int)
  </code></pre>
  
  <p>My service </p>
  
  <pre><code>//return a json with a list of files to download (and to more list -not important for now-)
   @GET("index.php")
      fun getMovimientos(@Query("r") r: String = "movimientos",
                         @Query("imei") id: String,
                         @Query("access-token") accesstoken: String): Observable&lt;Movimientos&gt;
  
  
      @Streaming
      @GET("index.php")
      fun getArchivo(@Query("r") r: String = "descarga",
                      @Query("id") fileID: String,
                      @Query("access-token") accesstoken: String): Observable&lt;ResponseBody&gt;
  
  
      companion object {
          fun create(): WikiApiService {
  
              val retrofit = Retrofit.Builder()
                      .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                      .addConverterFactory(GsonConverterFactory.create())
                      .baseUrl("https://url/api.biblioteca/")
                      .build()
  
              return retrofit.create(WikiApiService::class.java)
          }
      }
  </code></pre>
  
  <p>call for get the list to download and call the download process</p>
  
  <pre><code>
  private fun beginSearch(searchString: String) {
          disposable = wikiApiServe.getMovimientos("movimientos", "d55a374eebc242a5", "XXXXXXXXXXXX")
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
  
                  .subscribe(
                          {
                              result -&gt; txt_search_result.text = "${result.estructura.size} tamaño estructura"
                              Log.d("Estructura: ", "Es: ${result.estructura}")
                              //var aux =10
  
                              result.estructura.forEach {
                                  nodo -&gt;
                                  if (nodo.file/) {
                                      //aux--
                                      descargarArchivo(nodo.id)
  
                                  }
                              }
                          },
                          { error -&gt; Toast.makeText(this, error.message, Toast.LENGTH_SHORT).show() }
                  )
  
  
  
  
      }
  
  </code></pre>
  
  <p>call for the download function</p>
  
  <pre><code>private fun descargarArchivo(id: String){
          disposable = wikiApiServe.getArchivo("descarga", id, "XXXXXXXXXXX")
                  .subscribeOn(Schedulers.io())
                  .observeOn(Schedulers.newThread())
                  .subscribe(
                          {
                              //result -&gt; txt_search_result.text = "${result.estructura.size} tamaño estructura"
                              totalDescargas = totalDescargas +1
                              Log.d("Descargado: ", "Id archivo: $id bytes: ${it.bytes().size} Total descargas: $totalDescargas")
  
                              //total.text = totalDescargas
                          },
                          { error -&gt; Toast.makeText(this, error.message, Toast.LENGTH_SHORT).show() }
                  )
  
      }
  </code></pre>
  
  <p>When i run the code the app download some files (about five) and then do not do anything more (The logcat send me a lot of GC_FOR_ALLOC warinings and frame skip). I tried limit the calls to the download method (programmatically with a count variable (limit to 10), for avoid to try download the 200 files togheter) and works but still sending GC_FOR_ALLOC and i have to add some code to download the other 190 files in batches of ten (but i think this is not the best approach). im see thath i has to use I list of observers and call the downlad function with flatMap as another obsever. But i not understand how to work with this approach.</p>
  
  <p>I try to use another thread (a new one for each download) but i have an error with the thread pool</p>