title: Wait for Room @Insert query completion
body: <p>I can't figure out how to do a "simple" operation with Room and MVVM pattern. 
  I’m fetching some data with Retrofit. A “proper” response triggers an observer in the activity and a small part of the response itself is inserted in the database using Room library, wiping all previous values stored and inserting the fresh ones. Otherwise old values are retained on DB.
  Just after that, I would like to check for a field in the database, but I’m not able to force this operation to wait until the previous one is completed.</p>
  
  <p>Models</p>
  
  <pre><code>@Entity(tableName = "licence")
  data class Licence(
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "licence_id")
      var licenceId: Int = 0,
      @Ignore
      var config: List&lt;LicenceConfig&gt;? = null,
  .......
  //all the others attributes )
  
  @Entity(foreignKeys = [
  ForeignKey(
          entity = Licence::class,
          parentColumns = ["licence_id"],
          childColumns = ["licence_reference"],
          onDelete = ForeignKey.CASCADE
  )],tableName = "licence_configurations")
  data class LicenceConfig(
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "licence_config_id")
      var licenceConfigId: Int,
  
      @ColumnInfo(name="licence_reference")
      var licenceReference: Int,
  </code></pre>
  
  <p>Observer in the activity</p>
  
  <pre><code>loginViewModel.apiResponse.observe(this, Observer { response -&gt;
          response?.let {
               loginViewModel.insertLicences(response.licence)
          }
             //here I need to wait for the insertion to end
             loginViewModel.methodToCheckForTheFieldOnDatabase()
  })
  </code></pre>
  
  <p>ViewModel </p>
  
  <pre><code>fun insertLicences(licences: List&lt;Licence&gt;) = viewModelScope.launch {
      roomRepository.deleteAllLicences()
      licences.forEach { licence -&gt;
          roomRepository.insertLicence(licence).also { insertedLicenceId -&gt;
              licence.config?.forEach { licenceConfiguration -&gt;
                  roomRepository.insertLicenceConfiguration(
                          licenceConfiguration.apply { licenceReference = insertedLicenceId.toInt() }
                  )
              }
          }
      }
  }
  </code></pre>
  
  <p>Room Repository</p>
  
  <pre><code>class RoomRepository(private val roomDao: RoomDao) {
  
  val allLicences: LiveData&lt;List&lt;Licence&gt;&gt; = roomDao.getAllLicences()
  
  suspend fun insertLicence(licence: Licence): Long {
     return roomDao.insertLicence(licence)
     }
  
  suspend fun insertLicenceConfiguration(licenceConfiguration: LicenceConfig){
      return roomDao.insertLicenceConfiguration(LicenceConfig)
     }
  }
  </code></pre>
  
  <p>RoomDao</p>
  
  <pre><code>@Dao
  interface RoomDao {
  
  @Query("select * from licence")
  fun getAllLicences(): LiveData&lt;List&lt;Licence&gt;&gt;
  
  @Insert(onConflict = OnConflictStrategy.REPLACE)
  suspend fun insertLicence(licence: Licence): Long
  
  @Insert
  suspend fun insertLicenceConfiguration(licence: LicenceConfig)
  
  @Query("DELETE FROM licence")
  suspend fun deleteAllLicences()
  }
  </code></pre>
  
  <p>Set an observer to the "allLicences" LiveData or directly on that field on DB is not an option because the operations will be performed just after the activity creation and I have to wait until the API response to perform them. </p>
  
  <p>In another project, without Room, I have used async{} and .await() to perform sequential operations while working with coroutines but I can't really make it works here. When I pause the debugger just after the insertion method the value of "allLicences" it's always null but after resuming and exporting the DB the data are properly inserted. I also tried adding .invokeOnCompletion{} after the ViewModel method but with the same result.<br>
  Basically I would like to wait for this method to end to do another operation. </p>
  
  <p>Any suggestions? </p>
  
  <p>EDIT </p>
  
  <p>I totally forgot to report the models! Each licence have a list of configurations. When I perform a licence insert I take the autogenerated id, I apply it to the licenceConfig and then I perform the insert for each licenceConfig object (the code in the nested forEach loop of the ViewModel method). The problem seems to be that performing this nested loop breaks the "synchronicity" of the operation</p>