简体   繁体   English

将数据从 Android Kotlin API 记录到房间

[英]Recording data from Android Kotlin API to the room

I want to save data to Room.我想将数据保存到 Room。 There is a string, there is an int but there is also a List.有一个字符串,有一个 int,但也有一个 List。 I wonder if there is a way to save the listed structures in Room?我想知道是否有办法保存房间中列出的结构?

data class Model(
    var ranking: Int? = null,
    var team_name: String? = null,
    var coach: String? = null,
    var market_value: String? = null,
    var image: String? = null,
    var next_week_opponent: NextWeekOpponent? = null,
    var last_week_opponent: LastWeekOpponent? = null
)

Room can handle collections and based on what value is passed, it returns single row ID or list of all the row IDs inserted. Room 可以处理 collections 并根据传递的值返回单个行 ID 或插入的所有行 ID 的列表。 The response object needs be mapped with the entity.响应 object 需要与实体进行映射。 For example response data class:例如响应数据 class:

data class ApiResponse(
    val intValue: Int,
    val stringValue: String,
    val sources: List<SomeList>
)
val response = apiService.getSomeList()

appDatabase.someDummyDao.insertAll(response.someList)

Room can save/retrieve list.房间可以保存/检索列表。

  @Insert
    fun insertAll(vararg list: SomeList): List<long>

  @Query("select * from some_table")
    fun getSavedList(): List<SomeList>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM