Reputation: 31
My Type Converter
import androidx.room.TypeConverter
import com.amin.movie.vo.Query
object QueryTypeConverter {
@TypeConverter
@JvmStatic
fun queryToString(query: Query?): String? = if (query != null) Query.toJson(query) else null
@TypeConverter
@JvmStatic
fun stringToQuery(data: String?): Query? = if (data != null) stringToQuery(data) else null
}
and Entity is
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.amin.movie.db.MovieTypeConverters
import com.amin.movie.db.QueryTypeConverter
import com.google.gson.annotations.SerializedName
@Entity
@TypeConverters(QueryTypeConverter::class)
data class MovieSearchResult(
// and other fields
@PrimaryKey
val query: Query
)
that error happend when i want make my project 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.
Upvotes: 2
Views: 4404
Reputation: 51
You must be probably using suspend with functions in Dao class. Remove those suspend keywords in Dao class
Old Code
@Insert
suspend fun insertdata(attendance: Attendance)
@Query("SELECT * FROM attendance")
suspend fun getdata(attendance: Attendance):LiveData<List<Attendance>>
New Code (Without Suspend Keyword)
@Insert
fun insertdata(attendance: Attendance)
@Query("SELECT * FROM attendance")
fun getdata(attendance: Attendance):LiveData<List<Attendance>>
Upvotes: 0
Reputation: 39
I've tested your code on the latest release of Room at this time and it works for me. I've created a Database and a Dao to make sure it works and everything seems fine.
Even though your method now works, this isn't the one they show in the kotlin examples in the Room documentation for kotlin.
Here's their example on TypeConverter and this is how it would apply to your specific case (I've also provided the Database and Dao I've made):
// The query class wasn't provided so this is just a simple
// class with a toJson static method
data class Query(var query: String) {
companion object {
fun toJson(query: Query): String {
return query.query
}
}
}
@Entity
@TypeConverters(QueryTypeConverter::class)
data class MovieSearchResult(
// and other fields
@PrimaryKey
val query: Query
)
class QueryTypeConverter {
@TypeConverter
fun queryToString(query: Query?): String? = if (query != null) Query.toJson(query) else null
@TypeConverter
fun stringToQuery(data: String?): Query? = if (data != null) stringToQuery(data) else null
}
@Dao
interface MovieSearchResultDao {
@Insert
fun insert(r: MovieSearchResult)
@Update
fun update(r: MovieSearchResult)
@Delete
fun delete(r: MovieSearchResult)
}
@Database(entities=arrayOf(MovieSearchResult::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun dao(): MovieSearchResultDao
companion object {
private var DB: AppDatabase? = null;
public fun instance(context: Context): AppDatabase {
var db = DB
if (db == null) {
db = Room.databaseBuilder(
context,
AppDatabase::class.java, "database-name"
).build()
DB = db
}
return db
}
}
}
Upvotes: 1