title: How to handle query method's return type room using Kotlin, Coroutines, ViewModel, LiveData
body: <p>How to handle query method's return type room using Kotlin, Coroutines, ViewModel, LiveData</p>
  <p>the building is failed and I'm getting lots of errors that are pointing into my Dao class and the error is</p>
  <p>Error 1:</p>
  <blockquote>
  <p>Not sure how to handle query method's return type (java.lang.Object).
  DELETE query methods must either return void or int (the number of
  deleted rows).</p>
  </blockquote>
  <p>Error 2:</p>
  <blockquote>
  <p>error: Query method parameters should either be a type that can be
  converted into a database column or a List / Array that contains such
  type. You can consider adding a Type Adapter for this.
  kotlin.coroutines.Continuation&lt;? super kotlin.Unit&gt; continuation);</p>
  </blockquote>
  <p>Error 3:</p>
  <blockquote>
  <p>error: Unused parameter: continuation
  public abstract java.lang.Object clear(@org.jetbrains.annotations.NotNull()</p>
  </blockquote>
  <p>Error 4:</p>
  <blockquote>
  <p>error: Type of the parameter must be a class annotated with @Entity or
  a collection/array of it.
  kotlin.coroutines.Continuation&lt;? super kotlin.Unit&gt; continuation);</p>
  </blockquote>
  <p>Error 5:</p>
  <blockquote>
  <p>error: Not sure how to handle insert method's return type.
  public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()</p>
  </blockquote>
  <p>**<strong>Here is my full code:</strong>
  <a href="https://drive.google.com/drive/folders/1qWoud5XogzkTmpa-GWxLJStfdUPSoV7r?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/drive/folders/1qWoud5XogzkTmpa-GWxLJStfdUPSoV7r?usp=sharing</a></p>
  <p>android kotlin - Coroutines Room ViewModel LiveData
  MainActivity.kt</p>
  <pre><code>package com.example.coroutine
  
  import android.os.Bundle
  import android.text.method.ScrollingMovementMethod
  import androidx.appcompat.app.AppCompatActivity
  import androidx.lifecycle.Observer
  import androidx.lifecycle.ViewModelProvider
  import kotlinx.android.synthetic.main.activity_main.*
  import java.util.UUID
  import kotlin.random.Random
  
  
  class MainActivity : AppCompatActivity() {
  
      private lateinit var model: StudentViewModel
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          // make text view text scrollable
          textView.movementMethod = ScrollingMovementMethod()
  
          // initialize the student view model
          model = ViewModelProvider(this).get(StudentViewModel::class.java)
  
  
          // observe the students live data
          model.students.observe(this, Observer { students-&gt;
                  textView.text = &quot;Students(${students.size})...&quot;
  
                  students.forEach {
                      textView.append(&quot;\n${it.id} | ${it.fullName} : ${it.result}&quot;)
                  }
              }
          )
  
  
          btnInsert.setOnClickListener {
              // generate a new student
              val student = Student(
                  null,
                  UUID.randomUUID().toString(),
                  Random.nextInt(100)
              )
  
              // insert new student into room database
              model.insert(student)
          }
  
  
          btnClear.setOnClickListener {
              // delete all students from room student table
              model.clear()
          }
      }
  }
  </code></pre>
  <p>activity_main.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;#FAE6FA&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;com.google.android.material.button.MaterialButton
          android:id=&quot;@+id/btnInsert&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;8dp&quot;
          android:layout_marginTop=&quot;8dp&quot;
          android:backgroundTint=&quot;#8DB600&quot;
          android:text=&quot;Insert&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
      &lt;com.google.android.material.button.MaterialButton
          android:id=&quot;@+id/btnClear&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;8dp&quot;
          android:backgroundTint=&quot;#E52B50&quot;
          android:text=&quot;Clear&quot;
          app:layout_constraintBottom_toBottomOf=&quot;@+id/btnInsert&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/btnInsert&quot; /&gt;
  
      &lt;com.google.android.material.textview.MaterialTextView
          android:id=&quot;@+id/textView&quot;
          style=&quot;@style/TextAppearance.MaterialComponents.Subtitle1&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;0dp&quot;
          android:textColor=&quot;#1B1811&quot;
          android:textStyle=&quot;bold&quot;
          android:padding=&quot;8dp&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/btnInsert&quot;
          app:layout_constraintVertical_bias=&quot;1.0&quot;
          tools:text=&quot;TextView&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p>RoomSingleton.kt</p>
  <pre><code>package com.example.coroutine
  
  import androidx.room.Database
  import androidx.room.Room
  import androidx.room.RoomDatabase
  import android.content.Context
  
  @Database(entities = [Student::class], version = 1)
  abstract class RoomSingleton : RoomDatabase() {
      abstract fun studentDao():StudentDao
  
      companion object {
          private var INSTANCE: RoomSingleton? = null
          fun getInstance(context: Context): RoomSingleton {
              if (INSTANCE == null) {
                  INSTANCE = Room.databaseBuilder(
                      context,
                      RoomSingleton::class.java,
                      &quot;roomdb&quot;)
                      .build()
              }
              return INSTANCE as RoomSingleton
          }
      }
  }
  </code></pre>
  <p>RoomDao.kt</p>
  <pre><code>package com.example.coroutine
  
  import androidx.lifecycle.LiveData
  import androidx.room.Dao
  import androidx.room.Insert
  import androidx.room.OnConflictStrategy
  import androidx.room.Query
  
  @Dao
  interface StudentDao{
      @Query(&quot;SELECT * FROM studentTbl ORDER BY id DESC&quot;)
      fun getStudents():LiveData&lt;List&lt;Student&gt;&gt;
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      suspend fun insert(student:Student)
  
      @Query(&quot;DELETE FROM studentTbl&quot;)
      suspend fun clear()
  }
  </code></pre>
  <p>RoomEntity.kt</p>
  <pre><code>package com.example.coroutine
  
  import androidx.room.ColumnInfo
  import androidx.room.Entity
  import androidx.room.PrimaryKey
  
  @Entity(tableName = &quot;studentTbl&quot;)
  data class Student(
      @PrimaryKey
      var id:Long?,
  
      @ColumnInfo(name = &quot;uuid&quot;)
      var fullName: String,
  
      @ColumnInfo(name = &quot;result&quot;)
      var result:Int
  )
  </code></pre>
  <p>StudentViewModel.kt</p>
  <pre><code>package com.example.coroutine
  
  import androidx.lifecycle.AndroidViewModel
  import android.app.Application
  import androidx.lifecycle.LiveData
  import androidx.lifecycle.viewModelScope
  import kotlinx.coroutines.Dispatchers
  import kotlinx.coroutines.launch
  
  
  class StudentViewModel(application:Application): AndroidViewModel(application){
      private val db:RoomSingleton = RoomSingleton.getInstance(application)
  
      internal val students : LiveData&lt;List&lt;Student&gt;&gt; = db.studentDao().getStudents()
  
      fun insert(student: Student){
          viewModelScope.launch(Dispatchers.IO) {
              db.studentDao().insert(student)
          }
      }
  
      fun clear(){
          viewModelScope.launch(Dispatchers.IO) {
              db.studentDao().clear()
          }
      }
  }
  </code></pre>