How to handle query method's return type room using Kotlin, Coroutines, ViewModel, LiveData
the building is failed and I'm getting lots of errors that are pointing into my Dao class and the error is
Error 1:
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).
Error 2:
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<? super kotlin.Unit> continuation);
Error 3:
error: Unused parameter: continuation public abstract java.lang.Object clear(@org.jetbrains.annotations.NotNull()
Error 4:
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
Error 5:
error: Not sure how to handle insert method's return type. public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()
MainActivity I used declared the Views as lateinit's and late did the findViewById's:-
class MainActivity : AppCompatActivity() {
private lateinit var model: StudentViewModel
lateinit var textView: TextView
lateinit var btnClear: Button
lateinit var btnInsert: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = this.findViewById(R.id.textView)
btnClear = this.findViewById(R.id.btnClear)
btnInsert = this.findViewById(R.id.btnInsert)
// 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->
textView.text = "Students(${students.size})..."
students.forEach {
textView.append("\n${it.id} | ${it.fullName} : ${it.result}")
}
}
)
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()
}
}
}
Build Gradle (Module) used :-
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "a.a.so69998391kotlinroom_to_handle_query_return_types"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.room:room-ktx:2.4.0-beta01'
implementation 'androidx.room:room-runtime:2.4.0-beta01'
implementation 'androidx.lifecycle:lifecycle-common:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
kapt 'androidx.lifecycle:lifecycle-compiler:2.4.0'
kapt 'androidx.room:room-compiler:2.4.0-beta01'
}