简体   繁体   中英

Recording data from Android Kotlin API to the room

I want to save data to Room. There is a string, there is an int but there is also a 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. The response object needs be mapped with the entity. For example response data 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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