title: Android Room Kotlin - Query in background thread - return value problem
body: <p>I have converted a sample program from Java/SQLite to Kotlin/Room.</p>
  
  <p>I am struggling to implement queries with return values in a background thread.</p>
  
  <p>This has been asked but I could not get it to work.
  I have read answers to similar questions but some are deprecated, or some solutions seem complex for something that should be trivial. </p>
  
  <p>I am really stumped in coming up with a simple solution when I need to use the return value of the query.</p>
  
  <p>(Everything works as it should if I force doing queries in the main thread with allowMainThreadQueries())</p>
  
  <p>This is one of the functions that I would like to have do the query in a background thread:</p>
  
  <pre><code>fun getCrimes(): List&lt;Crime&gt; {
      val crimes = crimesDAO.getAllCrimes() as ArrayList&lt;Crime&gt;
      return crimes
  }
  </code></pre>
  
  <p>I can call the function like follows and it works but it would mean that I need to add Async calls throughout other classes and it does not seem elegant:</p>
  
  <pre><code>AsyncTask.execute {
      mCrimes = getCrimes() as ArrayList&lt;Crime&gt;
  }
  </code></pre>
  
  <p><strong>==> I would like to modify getCrimes itself to have it run the query in the background, like:</strong> (incorrect code follows)</p>
  
  <pre><code>fun getCrimes(): List&lt;Crime&gt; {
      var crimes: ArrayList&lt;Crime&gt;
  
      AsyncTask.execute {
          crimes = crimesDAO.getAllCrimes() as ArrayList&lt;Crime&gt;
      }
  
      return crimes // This is wrong as crimes in not initialized
  }
  </code></pre>
  
  <p>I looked into kotlin coroutines, Live Data and rxjava but could not find a simple way around this.</p>
  
  <h2>Background info:</h2>
  
  <p>This is the data class:</p>
  
  <pre><code>@Entity(tableName = "crimes_table")
  class Crime {
  
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name="id")
      var id: Long = 0
  
      @ColumnInfo(name="uuid")
      @TypeConverters(UUIDConverter::class)
      var mId: UUID = UUID.randomUUID()
  
      @ColumnInfo(name="title")
      var mTitle: String? = null
  
      @ColumnInfo(name="date")
      @TypeConverters(DateConverter::class)
      var mDate: Date? = Date()
  
      @ColumnInfo(name="solved")
      var mSolved: Boolean = false
  }
  </code></pre>
  
  <p>This is the DAO:</p>
  
  <pre><code>@Dao
  interface CrimesListDAO {
  
      @Query("SELECT * FROM crimes_table")
      fun getAllCrimes(): List&lt;Crime&gt;
  
      @Query("SELECT * FROM crimes_table WHERE uuid = :uuidString LIMIT 1")
      fun getOneCrime(uuidString: String): Crime
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      fun insertCrime(crime: Crime)
  
      @Update(onConflict = OnConflictStrategy.REPLACE)
      fun updateCrime(crime: Crime)
  
      @Delete
      fun deleteCrime(crime: Crime)
  }
  </code></pre>
  
  <p>This is the DatabaseApp class:</p>
  
  <pre><code>@Database(entities = [(Crime::class)], version = 1)
  abstract class AppDatabase : RoomDatabase() {
      abstract fun crimesListDAO(): CrimesListDAO
  }
  </code></pre>
  
  <p>This is how I instantiate the database:</p>
  
  <pre><code>class ApplicationContextProvider : Application() {
      ...
      companion object {
          var database: AppDatabase? = null
      ...
      }
  
      override fun onCreate() {
          super.onCreate()
          ApplicationContextProvider.database = Room.databaseBuilder(this, AppDatabase::class.java, "crimeBase.db").build()
      }
  }
  </code></pre>