Reputation: 83
In my app I have a model class, it has some variables, I can call and show this data in this app using retrofit and room DB. which means this app first collects data from the server then it shows in room DB. But when I am using the list in this model class it shows this error. Here is my code
Movie.kt
@Entity(tableName = "movie_table")
data class Movie(
@SerializedName("Id")
@PrimaryKey(autoGenerate = true)
val id: Int,
@SerializedName("Title")
@Expose
val title: String,
@SerializedName("Year")
@Expose
val Year: Int,
@SerializedName("Actors")
@Expose
val actorDetails: List<Actor>
)
Actor.kt
data class Actor(
@SerializedName("ActorName")
@Expose
val actorName: String,
@SerializedName("ActorPoster")
@Expose
val actorImage: String
)
MovieDao.kt
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovie(movie: Movie)
@Query("SELECT * FROM movie_table")
suspend fun getAllMovieDB(): List<Movie>
}
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
here is my fake JSON API enter link description here
here is the error enter image description here I can't find any error, I am also using analyze for get the error but it's show nothing. How can I solve this? Thank you.
Upvotes: 2
Views: 35705
Reputation: 5205
If you're facing this error while implementing dagger 2 in your new project.
Check your kotlin version in project-level build.gradle If it is 1.6.0 change it to 1.4.31 or 1.4.32
Upvotes: 0
Reputation: 402
Just add the below lines to "gradle.properties"
kapt.use.worker.api=false
kapt.incremental.apt=false
The above solution is working for me.
Upvotes: 5
Reputation: 507
In my case there was an issue with the jdk version.
After these steps it worked for me
Upvotes: 0
Reputation: 21
In my case I just update the kotlin version in my build.gradle (project) it should be like this:
buildscript {
ext.kotlin_version = "1.4.21"
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Upvotes: 1
Reputation: 142
at first you have to use @TypeConverter from room because room can not insert the custom types like List or object or bitmap. So firstly make a class named converter then add this class in database using annotation @TypeConverters. here is the code
Converter.kt
class Converter {
@TypeConverter
fun fromActor(actor: List<Actor>):String{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().toJson(actor,type)
}
@TypeConverter
fun toActor(actorString: String): List<Actor>{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().fromJson<List<Actor>>(actorString,type)
}
}
and finally add this converter.kt class in your database
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
@TypeConverters(Converter::class)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
Thank you.
Upvotes: 3