Saving and retrieving nested objects in Room database

帅比萌擦擦* 提交于 2020-08-10 20:22:03

问题


I want to save nested objects from API response into Room database so that I can retrieve them quickly next time the app is launched. The structure is the following, A root object that has fields which are themselves objects and contains some list of other objects, however all end up sharing same final object. How can I save the whole data into Room and be able to retrieve them back?

Here's how it looks like:

1.Top most class:

@Parcelize
data class HomeResponse(
    @field:SerializedName("video")
    val video: Video? = null,
    @field:SerializedName("statut")
    val statut: String? = null
) : Parcelable

@Parcelize
data class Video(
    @field:SerializedName("emission")
    val emission: Emission? = null,
    @field:SerializedName("music")
    val music: Music? = null,
    @field:SerializedName("serie")
    val serie: Serie? = null,
    @field:SerializedName("movie")
    val film: Movie? = null,
    @field:SerializedName("gag")
    val gag: Gag? = null
) : Parcelable

@Parcelize
data class Serie(
    @field:SerializedName("count")
    val nbre: Int? = null,
    @field:SerializedName("rows")
    val rows: List<RowsItem?>? = null
) : Parcelable

@Parcelize
data class Emission(
    @field:SerializedName("count")
    val nbre: Int? = null,
    @field:SerializedName("rows")
    val rows: List<RowsItem?>? = null
) : Parcelable

@Parcelize
data class Movie(
    @field:SerializedName("count")
    val nbre: Int? = null,
    @field:SerializedName("rows")
    val rows: List<RowsItem?>? = null
) : Parcelable

@Parcelize
data class Gag(
    @field:SerializedName("count")
    val nbre: Int? = null,
    @field:SerializedName("rows")
    val rows: List<RowsItem?>? = null
) : Parcelable


@Parcelize
data class Music(
    @field:SerializedName("count")
    val nbre: Int? = null,
    @field:SerializedName("rows")
    val rows: List<RowsItem?>? = null
) : Parcelable

@Parcelize
data class RowsItem(
    @field:SerializedName("id")
    val id: String? = null,
    @field:SerializedName("poster")
    val poster: String? = null,
    @field:SerializedName("title")
    val title: String? = null
) : Parcelable

How to save this into Room? Which classes will be annotated as Entity? Do I have to create DAO for each object/class?

Thanks for your time.

Note, the code was generated from plugin.

来源:https://stackoverflow.com/questions/62595395/saving-and-retrieving-nested-objects-in-room-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!