Reputation: 283
I am trying to implement Room in my android application. I am trying to get data from API and save them in local database. But when I run the application this error occurred
e: [kapt] An exception occurred: java.lang.IllegalArgumentException: intcannot be converted to an Element
This is my database class:
@Database(entities = [
(User::class)],
version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: UserDatabase? = null
fun getDatabase(context: Context): UserDatabase {
if (INSTANCE == null){
synchronized(this){
INSTANCE = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database")
.build()
}
}
return INSTANCE!!
}
}
}
This is my Repository:
class Repository(application: Application) {
private var mUserDao: UserDao
init {
val db = UserDatabase.getDatabase(application)
}
@WorkerThread
fun createUser(user: User) {
mUserDao.createUser(user)
}
@WorkerThread
fun deleteUser(id: Int) {
mUserDao.deleteUser(id)
}
@WorkerThread
fun getUser(id: Int) {
mUserDao.getUser(id)
}
And here is my userDao
@Dao
interface UserDao{
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun createUser(user: User)
@Delete
fun deleteUser(int: Int)
@Query("SELECT * FROM user WHERE user_id = :userID")
fun getUser(userID: Int): LiveData<User>
}
My LogginViewModel:
class LoginViewModel(application: Application) : AndroidViewModel(application) {
var repository: Repository = Repository(application)
fun createUser(user: User) = repository.createUser(user)
}
Also this error get occurred:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin' <30 internal calls> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<1 internal call> at java.lang.Thread.run(Thread.java:745) Caused by: org.gradle.api.GradleException: Compilation error. See log for more details at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16) at org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17 internal calls> ...33 more
Upvotes: 2
Views: 3708
Reputation: 6862
The problem lies in your @Delete
@Delete
fun deleteUser(int: Int)
Which should be
@Delete
fun deleteUser(user: User)
The parameter for @Delete
method should be an entity or an array of an entity, not an Int
Upvotes: 10
Reputation: 283
This is the solution:
As @musooff mentioned above I changed the delete method like this:
@Delete
fun deleteUser(user: User)
or if you want to delete a user with a specific id you do this query:
@Query("DELETE FROM user WHERE user_id = :id")
fun deleteUser(id: Int)
Then to solve next error you should execute methods in UserDao inside an Asynctask in Repository file for example for creating user I did this:
private class insertAsyncTask internal constructor(private val mAsyncTaskDao: UserDao) : AsyncTask<User, Void, Void>() {
override fun doInBackground(vararg params: User): Void? {
mAsyncTaskDao.createUser(params[0])
return null
}
}
Then createUser function in Repository goes like this:
fun createUser(user: User) {
insertAsyncTask(mUserDao).execute(user)
}
This solved the problem for me.
Upvotes: 3