英文:
Suspend keyword causing errror while inserting in room database kotin(android)
问题
我在将数据插入到Room数据库时遇到以下错误:
> 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);
这是我的DAO:
@Dao
interface FavDishDao {
    @Insert
    fun insertFavDishDetails(favDish: FavDish)
    @Query("SELECT * FROM FAV_DISHES_TABLE ORDER BY ID")
    fun getAllDishesList(): Flow<List<FavDish>>
}
build.gradle文件:
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}
android {
    namespace 'pro.shivanshtariyal.recipeapp'
    compileSdk 33
    defaultConfig {
        applicationId "pro.shivanshtariyal.recipeapp"
        minSdk 21
        targetSdk 33
        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
    }
    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}
dependencies {
    ...
    def room_version='2.2.6'
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation 'com.github.bumptech.glide:compiler:4.12.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
我尝试移除了suspend关键字,应用程序编译通过,但当我尝试保存数据时,出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pro.shivanshtariyal.recipeapp, PID: 5274
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
FavDish文件:
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "fav_dishes_table")
data class FavDish(
    @ColumnInfo val image: String,
    @ColumnInfo(name = "image_source") val imageSource: String, // Local or Online
    @ColumnInfo val title: String,
    @ColumnInfo val type: String,
    @ColumnInfo val category: String,
    @ColumnInfo val ingredients: String,
    @ColumnInfo(name = "cooking_time") val cookingTime: String,
    @ColumnInfo(name = "instructions") val directionToCook: String,
    @ColumnInfo(name = "favorite_dish") var favoriteDish: Boolean = false,
    @PrimaryKey(autoGenerate = true) val id: Int = 0
)
英文:
I am getting the following error when inserting in room database
>
> 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);
This is my DAO
@Dao
interface FavDishDao {
    @Insert
    fun insertFavDishDetails(favDish: FavDish)
    @Query("SELECT * FROM FAV_DISHES_TABLE ORDER BY ID")
    fun getAllDishesList(): Flow<List<FavDish>>
}
build.gradle files
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
 }
android {
namespace 'pro.shivanshtariyal.recipeapp'
compileSdk 33
defaultConfig {
applicationId "pro.shivanshtariyal.recipeapp"
minSdk 21
targetSdk 33
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
    }
packagingOptions{
exclude 'META-INF/atomicfu.kotlin_module'
   }
kotlinOptions {
jvmTarget = '1.8'
    }
buildFeatures {
viewBinding true
  } }
dependencies {
...
def room_version='2.2.6'
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
 `plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
 }`
I tried to remove the suspend keyword, the app was build but when I tried to save the data I got the following error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: pro.shivanshtariyal.recipeapp, PID: 5274
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:267)
FavDish file
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "fav_dishes_table")
data class FavDish(
    @ColumnInfo val image: String,
    @ColumnInfo (name = "image_source") val imageSource: String, // Local or Online
    @ColumnInfo val title: String,
    @ColumnInfo val type: String,
    @ColumnInfo val category: String,
    @ColumnInfo val ingredients: String,
    // Specifies the name of the column in the table if you want it to be different from the name of the member variable.
    @ColumnInfo(name = "cooking_time") val cookingTime: String,
    @ColumnInfo(name = "instructions") val directionToCook: String,
    @ColumnInfo(name = "favorite_dish") var favoriteDish: Boolean = false,
    @PrimaryKey(autoGenerate = true) val id: Int = 0
)
答案1
得分: 0
你之所以出现错误是因为你试图从主/UI线程写入数据库。你必须从另一个线程中进行以避免阻塞UI。
要修复这个问题,你必须创建一个协程作用域,然后从那里插入到数据库。
如果你在Fragment或Activity中,可以使用以下模式:
        lifecycleScope.launch {
            withContext(Dispatchers.IO) {
                insertFavDishDetails(favDish)
            }
        }
如果你在ViewModel中,可以采取以下方式:
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                insertFavDishDetails(favDish)
            }
        }
英文:
You get the error because you are trying to write the database from the Main/UI thread. You must do it from another thread to avoid blocking the UI.
To fix this, you must create a coroutine scope and insert to the database from there.
If you are in Fragment or Activity, use the following pattern:
        lifecycleScope.launch {
            withContext(Dispatchers.IO) {
                insertFavDishDetails(favDish)
            }
        }
If you are in a ViewModel do the following:
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                insertFavDishDetails(favDish)
            }
        }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论