In an attempt to use Kotlin Coroutines with Room Database, I used suspend for DAO methods as shown below:
@Entity(tableName = SPY_HINT_TABLE)
data class SpyHint(
@PrimaryKey(autoGenerate = true)
val id: Int,
val hintName: String,
val hintAnswer: String,
val author: String,
val rating: Double,
var wins: Int? = 0,
var attempts: Int? = 0,
val imageUrl: String
)
@Dao
interface HintDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertHint(hint: SpyHint)
@Query("Select * from $SPY_HINT_TABLE")
fun getHints(): LiveData<List<SpyHint>>
@Query("Select * from $SPY_HINT_TABLE where id=:id")
fun getHint(id: Int): LiveData<SpyHint>
@Update
suspend fun updateHint(hint: SpyHint)
}
But even though everything looked right, building the above code kept giving me the following error:
Type of the parameter must be a class annotated with @Entity or a collection/array of it
My app level build.gradle consists of the following for room dependencies:
implementation "androidx.room:room-runtime:2.2.5"
implementation "androidx.room:room-ktx:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
The problem was that in my build.gradle, the version of Kotlin I was using was 1.5.0
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.0"
But as far as I understood, this version of Kotlin along with Room and coroutines doesn’t work well.
Was able to resolve the issue with downgrading the Kotlin version to
1.4.32
Note: If someone is able to find a more appropriate solution or figure out why this version won’t work together, please reach out. But hoping this post saves someone many hours of trying to figure out above error.