title: How to insert a List of objects from an API to Room using Kotlin?
body: <p>In my app i have a simple Room DataBase with methods like update, insert, delete, and <code>insertOrUpdate</code> which check if same item with primary key exist and just sum it's quantity with new quantity.</p>
  <p>The data till now was added by user input, but now the user will be ever able to Sync the data from a remote server, so i've made an API which return the same array of objects as my table is structured, i've done all to get the list in the correct object class but now which would be the best way to use my <code>insertOrUpdate</code> function on that List of objects? should i loop throw each item and add it one by one?</p>
  <p>Which would be the best solution to do it?</p>
  <p>Here is my code of the DAO:</p>
  <pre><code>@Dao
  interface ArticoliDAO {
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      suspend fun insert(articolo: Articolo)
  
      @Update
      suspend fun update(articolo: Articolo)
  
      @Query(&quot;SELECT * FROM articoli_letti_table WHERE barcode = :key&quot;)
      suspend fun get(key: String): Articolo?
  
      @Query(&quot;UPDATE articoli_letti_table SET qta = qta + :qta WHERE barcode = :barcode&quot;)
      suspend fun updateQuantity(qta: Int, barcode: String)
  
      suspend fun insertOrUpdate(articolo: Articolo) {
          val itemFromDB = get(articolo.barcode)
          if (itemFromDB == null) {
              insert(articolo)
          }else {
              updateQuantity(articolo.qta, articolo.barcode)
          }
  
      }
  
      @Query(&quot;DELETE FROM articoli_letti_table WHERE barcode = :key&quot;)
      suspend fun delete(key: String)
  
      @Query(&quot;DELETE FROM articoli_letti_table&quot;)
      suspend fun clear()
  
      @Query(&quot;SELECT * FROM articoli_letti_table&quot;)
      fun getAll(): Flow&lt;List&lt;Articolo&gt;&gt;
  
  }
  </code></pre>
  <p>And here is my code from SettingActivity where the user can press the button &quot;Sync&quot; where all items from the server should be added to the database:</p>
  <pre><code>override fun onPreferenceTreeClick(preference: Preference?): Boolean {
      val key = preference?.key
  
      return when (key) {
          &quot;sync&quot; -&gt; {
              val moshi = Moshi.Builder()
                  .add(KotlinJsonAdapterFactory())
                  .build()
              val retrofit = Retrofit.Builder()
                  .baseUrl(&quot;http://192.168.100.65/VisualIntelligence/&quot;)
                  .addConverterFactory(MoshiConverterFactory.create(moshi))
                  .build()
              val service = retrofit.create(ArticoliService::class.java)
              val call = service.getArticoli()
              call.enqueue(object : Callback&lt;List&lt;Articolo&gt;&gt; {
                  override fun onResponse(
                      call: Call&lt;List&lt;Articolo&gt;&gt;,
                      response: Response&lt;List&lt;Articolo&gt;&gt;
                  ) {
                      if (response.code() == 200) {
  
                      }
                  }
  
                  override fun onFailure(call: Call&lt;List&lt;Articolo&gt;&gt;, t: Throwable) {
                      Log.e(&quot;ERR&quot;, t.message.toString())
                  }
  
              })
  
              Snackbar.make(requireView(), &quot;Sincronizzo gli articoli...&quot;, Snackbar.LENGTH_LONG).show()
              true
          }
          else -&gt; true
      }
  }
  </code></pre>