问题
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