I'm creating a project with Room and I have the following codes:
For my entity class:
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity("matches")
data class MatchEntity(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Int,
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "city") val city: String,
@ColumnInfo(name = "distance") val distance: Int,
@ColumnInfo(name = "description") val description: String,
@ColumnInfo(name = "score") val score: Int,
@ColumnInfo(name = "age") val age: Int,
@ColumnInfo(name = "body_type") val bodyType: BodyType,
@ColumnInfo(name = "height") val height: Float,
@ColumnInfo(name = "diet") val diet: DietType,
@ColumnInfo(name = "zodiac_sign") val zodiacSign: ZodiacSign,
@ColumnInfo(name = "religion") val religion: Religion,
@ColumnInfo(name = "drinking_habit") val drinkingHabit: DrinkingHabit,
@ColumnInfo(name = "smoking_habit") val smokingHabit: SmokingHabit,
@ColumnInfo(name = "mbti_type") val mbtiType: MbtiType,
@ColumnInfo(name = "enneagram") val enneagram: Enneagram,
@ColumnInfo(name = "kids_type") val kidsType: KidsType,
)
For my DAO class:
@Dao
interface MatchDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(matchEntity: MatchEntity): Long
@Update
suspend fun update(matchEntity: MatchEntity): Int
@Delete
suspend fun delete(matchEntity: MatchEntity)
@Query("SELECT * FROM matches")
suspend fun getAll(): List<MatchEntity>
@Query("SELECT * FROM matches WHERE id = :id")
suspend fun getById(id: Int): List<MatchEntity>
}
For my RoomDatabase class:
@Suppress("TooManyFunctions")
@Database(
entities = [
MatchEntity::class, MatchIceBreakerEntity::class, MatchInterestEntity::class,
MatchScheduleEntity::class, MatchSocialEntity::class, MatchStoryEntity::class,
UserEntity::class, UserIceBreakerEntity::class, UserInterestEntity::class,
UserSocialEntity::class, UserStoryEntity::class
],
exportSchema = false,
version = 1
)
@TypeConverters(
BodyTypeConverter::class,
DietTypeConverter::class,
DrinkingHabitConverter::class,
EnneagramConverter::class,
KidsTypeConverter::class,
MbtiTraitConverter::class,
MbtiTypeConverter::class,
ReligionConverter::class,
SleepingHabitConverter::class,
SmokingHabitConverter::class,
SocialConverter::class,
SocialMediaFrequencyConverter::class,
WorkoutTypeConverter::class,
ZodiacSignConverter::class
)
abstract class WingmanDatabase : RoomDatabase() {
abstract fun matchDao(): MatchDao
abstract fun matchIceBreakerDao(): MatchIceBreakerDao
abstract fun matchInterestDao(): MatchInterestDao
abstract fun matchScheduleDao(): MatchScheduleDao
abstract fun matchSocialDao(): MatchSocialDao
abstract fun matchStoryDao(): MatcStoryDao
abstract fun userDao(): UserDao
abstract fun userIceBreakerDao(): UserIceBreakerDao
abstract fun userInterestDao(): UserInterestDao
abstract fun userSocialDao(): UserSocialDao
abstract fun userStoryDao(): UserStoryDao
companion object {
const val DATABASE_NAME: String = "wingman_db"
}
}
For my dependencies:
plugins {
...
id 'kotlin-kapt'
}
...
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
...
dependencies {
...
def room_version = "2.5.1"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
For my build.gradle plugins:
plugins {
id 'com.android.application' version '8.0.1' apply false
id 'com.android.library' version '8.0.1' apply false
id 'androidx.navigation.safeargs' version '2.5.3' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0-Beta' apply false
}
I've worked with Room a lot of times, but somehow this time I'm getting this error:
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:27: error: Not sure how to convert a Cursor to this method's return type (java.lang.Object).
public abstract java.lang.Object getAll(@org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:28: 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 java.util.List<com.hikarisource.wingman.data.local.entity.MatchEntity>> $completion);
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:27: error: Unused parameter: $completion
public abstract java.lang.Object getAll(@org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:32: error: Not sure how to convert a Cursor to this method's return type (java.lang.Object).
public abstract java.lang.Object getById(int id, @org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:33: 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 java.util.List<com.hikarisource.wingman.data.local.entity.MatchEntity>> $completion);
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:32: error: Unused parameter: $completion
public abstract java.lang.Object getById(int id, @org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:11: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super java.lang.Long> $completion);
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:9: error: Not sure how to handle insert method's return type.
public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:21: error: Not sure how to handle delete method's return type. Currently the supported return types are void, int or Int.
public abstract java.lang.Object delete(@org.jetbrains.annotations.NotNull
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:23: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> $completion);
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:17: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super java.lang.Integer> $completion);
C:\Users\Leona\workspace\wingman\app\build\tmp\kapt3\stubs\debug\com\hikarisource\wingman\data\local\dao\MatchDao.java:15: error: Not sure how to handle update method's return type. Currently the supported return types are void, int or Int.
public abstract java.lang.Object update(@org.jetbrains.annotations.NotNull
This is the code it's been generated by MatchDao:
@kotlin.Metadata(mv = {1, 9, 0}, k = 1, xi = 48, d1 = {"\u00000\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\b\u0003\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0002\bg\u0018\u00002\u00020\u0001J\u0019\u0010\u0002\u001a\u00020\u00032\u0006\u0010\u0004\u001a\u00020\u0005H\u00a7@\u00f8\u0001\u0000\u00a2\u0006\u0002\u0010\u0006J\u0017\u0010\u0007\u001a\b\u0012\u0004\u0012\u00020\u00050\bH\u00a7@\u00f8\u0001\u0000\u00a2\u0006\u0002\u0010\tJ\u001f\u0010\n\u001a\b\u0012\u0004\u0012\u00020\u00050\b2\u0006\u0010\u000b\u001a\u00020\fH\u00a7@\u00f8\u0001\u0000\u00a2\u0006\u0002\u0010\rJ\u0019\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u0004\u001a\u00020\u0005H\u00a7@\u00f8\u0001\u0000\u00a2\u0006\u0002\u0010\u0006J\u0019\u0010\u0010\u001a\u00020\f2\u0006\u0010\u0004\u001a\u00020\u0005H\u00a7@\u00f8\u0001\u0000\u00a2\u0006\u0002\u0010\u0006\u0082\u0002\u0004\n\u0002\b\u0019\u00a8\u0006\u0011"}, d2 = {"Lcom/hikarisource/wingman/data/local/dao/MatchDao;", "", "delete", "", "matchEntity", "Lcom/hikarisource/wingman/data/local/entity/MatchEntity;", "(Lcom/hikarisource/wingman/data/local/entity/MatchEntity;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "getAll", "", "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", "getById", "id", "", "(ILkotlin/coroutines/Continuation;)Ljava/lang/Object;", "insert", "", "update", "app_debug"})
@androidx.room.Dao
public abstract interface MatchDao {
@androidx.room.Insert(onConflict = 1)
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull
com.hikarisource.wingman.data.local.entity.MatchEntity matchEntity, @org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super java.lang.Long> $completion);
@androidx.room.Update
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object update(@org.jetbrains.annotations.NotNull
com.hikarisource.wingman.data.local.entity.MatchEntity matchEntity, @org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super java.lang.Integer> $completion);
@androidx.room.Delete
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object delete(@org.jetbrains.annotations.NotNull
com.hikarisource.wingman.data.local.entity.MatchEntity matchEntity, @org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super kotlin.Unit> $completion);
@androidx.room.Query(value = "SELECT * FROM matches")
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object getAll(@org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super java.util.List<com.hikarisource.wingman.data.local.entity.MatchEntity>> $completion);
@androidx.room.Query(value = "SELECT * FROM matches WHERE id = :id")
@org.jetbrains.annotations.Nullable
public abstract java.lang.Object getById(int id, @org.jetbrains.annotations.NotNull
kotlin.coroutines.Continuation<? super java.util.List<com.hikarisource.wingman.data.local.entity.MatchEntity>> $completion);
}
I changed my table's name to martches
instead of match
because aparently it was undestanding that match
was a reserved keyword.
Also, I tried and see if there was some mistake made with my anotations, but I could not find it.
I also tried to remove the suspend
from all my DAOs and although I stop having compile time erros, I started getting an error on run time:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
If anyone know what is happening, I'll apreciate the help :)
The solution was to remove the suspend
modifier. Apparently, Room ditched its use even when the return type is not a DataStream like LiveData or Flow.
So now my DAO class looks like this:
@Dao
interface MatchDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(matchEntity: MatchEntity): Long
@Update
fun update(matchEntity: MatchEntity): Int
@Delete
fun delete(matchEntity: MatchEntity)
@Query("SELECT * FROM matches")
fun getAll(): List<MatchEntity>
@Query("SELECT * FROM matches WHERE id = :id")
fun getById(id: Int): List<MatchEntity>
}
Just be careful, as you should not call it on the Main thread, so do not forget to change your dispatcher to IO.