Android 房间类型转换器不工作

Android Room TypeConverter not working

我试图在我的 Room 数据库中保存一些数据,但它一直向我显示错误,代码如下:

MovieDao.kt

@Dao
interface MoviesDao {
    @Query("SELECT * from movie")
    fun getAll() : LiveData<List<Movie>>

    @Update
    fun update(movie: Movie)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(movie: Movie)
}

MoviesDatabase.kt

@Database(entities = [Movie::class], version = 1, exportSchema = false)
@TypeConverters(TorrentConverter::class, GenreConverter::class)
abstract class MoviesDatabase : RoomDatabase() {
    companion object {
        private var instance: MoviesDatabase? = null
        fun getDatabase(context: Context) : MoviesDatabase{
            if(instance == null) {
                instance = Room.databaseBuilder(context.applicationContext, MoviesDatabase::class.java,
                    "movies_database").build()
            }
            return instance as MoviesDatabase
        }
    }
    abstract fun getMoviesDao() : MoviesDao
}

MovieModels.kt

 @Entity(tableName = "movie")
    data class Movie(
        val url: String,
        @PrimaryKey
        val imdb_code: String,
        val title: String,
        val year: Int,
        val rating: Float,
        val runtime: Int,
        @TypeConverters(GenreConverter::class)
        val genres: List<String>?,
        val synopsis: String,
        val yt_trailer_code: String,
        val language: String,
        val mpa_rating: String,
        val medium_cover_image: String,
        val large_cover_image: String,
        val state: String,
        @TypeConverters(TorrentConverter::class)
        var torrents: List<Torrent>,
        var saved: Boolean = false,
        var marked: Boolean = false
    ) : Serializable

    data class Torrent(
        val url: String,
        val hash: String,
        val quality: String,
        val seeds: Int,
        val peers: Int,
        val size: String,
        val size_bytes: Long
    ) : Serializable

TypeConverters.kt

class TorrentConverter {
    @TypeConverter
    fun toTorrent(json: String): Torrent {
        val type = object : TypeToken<Torrent>() {}.type
        return Gson().fromJson(json, type)
    }

    @TypeConverter
    fun toJson(torrent: Torrent) = Gson().toJson(torrent)
}

class GenreConverter {
    @TypeConverter
    fun toGenre(json: String): List<String> {
        val type = object : TypeToken<List<String>>() {}.type
        return Gson().fromJson(json, type)
    }

    @TypeConverter
    fun toJson(genres: List<String>) = Gson().toJson(genres)
}

错误告诉我:

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it. private java.util.List torrents;

谁能帮我弄清楚为什么会出现这个错误?非常感谢。

发生这种情况是因为您的 TorrentConverter returning 获取了错误的类型。 方法 toTorrent 应该 return 一个 List<Torrent> 而方法 toJson 应该接收一个 List<Torrent> 试试这个类型转换器:

class TorrentConverter {
    @TypeConverter
    fun toTorrent(json: String): List<Torrent> {
        val type = object : TypeToken<List<Torrent>>() {}.type
        return Gson().fromJson(json, type)
    }

    @TypeConverter
    fun toJson(torrent: List<Torrent>): String {
        val type = object: TypeToken<List<Torrent>>() {}.type
        return Gson().toJson(torrent, type)
    }
}