gpt4 book ai didi

android - 如何通过自定义列表在房间中创建TypeConverter?

转载 作者:行者123 更新时间:2023-12-02 13:42:43 25 4
gpt4 key购买 nike

我做了一个TypeConverter,但出现错误

Unable to create converter for class .models.lastanime.EpisodesEntityfor method EpisodesApi.getEpisodes



我不能完全理解如何制作TypeConverter,我已经做到了,我知道实现位置正确,因为我在编译中没有问题,但是由于出现错误而无法加载数据,看来它没有保存在房间数据库中

TYPE CONVERTER
class ListStringConverter {

@TypeConverter
fun fromString(value: String?): List<ServerEntity> {
val listType = object :
TypeToken<List<ServerEntity?>?>() {}.type
return Gson()
.fromJson<List<ServerEntity>>(value, listType)
}

@TypeConverter
fun listToString(list: List<ServerEntity?>?): String {
val gson = Gson()
return gson.toJson(list)
}

}

模型主题实体
data class EpisodesEntity(
@SerializedName("episodes")
val episodes: List<EpisodeEntity>

)

模型典范实体
@Entity
data class EpisodeEntity(

@PrimaryKey(autoGenerate = true)
val id: Int,

@SerializedName("poster")
@ColumnInfo(name = "episode")
val episode: Int?,

@SerializedName("poster")
@ColumnInfo(name = "poster")
val poster: String?,

@SerializedName("servers")
@ColumnInfo(name = "servers")
val servers: List<ServerEntity>?,

@SerializedName("title")
@ColumnInfo(name = "title")
val title: String?

)

除了所有模型外,“服务器”列表还使我难以将其插入房间
@SerializedName("servers")
@ColumnInfo(name = "servers")
val servers: List<ServerEntity>?,

API存储库
interface LastEpisodesRepository {

fun lastEpisodes(): Flow<Either<Failure, List<Episode>>>

class Network(
private val networkHandler: NetworkHandler,
private val service: LastEpisodesService,
private val local: EpisodeLocal
) : LastEpisodesRepository {

val preferences by lazy { SharedPrefsHelpers() }

override fun lastEpisodes(): Flow<Either<Failure, List<Episode>>> =
flow {

val days = local.getEpisodes()
val time = preferences.getLong(LocalShared.LastAnimes.lastepisodes, 0L)

if (days.isNullOrEmpty() || time == 0L || isFetchCurrentNeeded(time)) {
emit(getRemoteDay())
} else {
emit(Either.Right(local.getEpisodes().map { it.toEpisode() }))
}
}.catch {
emit(Either.Left(Failure.CustomError(ServiceKOs.DATABASE_ACCESS_ERROR, "DB Error")))
}.flowOn(Dispatchers.IO)


private fun getRemoteEpisode(): Either<Failure, List<Episode>> =
when (networkHandler.isConnected) {
true -> request(
service.getEpisodes(),
{ episodeEntity ->

val episodeList: List<EpisodeEntity> = episodeEntity.episodes

preferences.saveLong(LocalShared.LastAnimes.lastepisodes, Date().time)

addAllEpisodes(episodeList)
episodeList.map { it.toEpisode() }

},
EpisodesEntity(emptyList())
)
false, null -> Either.Left(Failure.NetworkConnection())
}

private fun addAllEpisodes(episodes: List<EpisodeEntity>) {
for (episode in episodes) {
local.addEpisodes(episode)
}
}

}

Room是从本地变量进行的调用,应用程序检查是否有下载的数据,如果没有下载的数据,它将调用服务,返回数据,同时将其保存在Room数据库中。

最佳答案

您不能有一个实体持有另一个实体的List。您需要定义a one-to-many relation between them

关于android - 如何通过自定义列表在房间中创建TypeConverter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61963144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com