我想从远程获取新闻并将网络响应Map到实体类以存储在存储库层的房间数据库中。我正在使用远程和数据库单独的数据类
下面是我正在使用的数据类....
如何添加类似下面的Map逻辑?
fun NewsRemote.toLocal() = NewsLocal(
..............
)
网络响应数据类
data class NewsRemote(
val articles: List<ArticlesItem?>? = null,
val status: String? = null,
) {
data class ArticlesItem(
val publishedAt: String? = null,
val author: String? = null,
val urlToImage: String? = null,
val description: String? = null,
val source: Source? = null,
val title: String? = null,
val url: String? = null,
val content: String? = null
) {
data class Source(
val name: String? = null,
val id: String? = null
)
}
}
数据库数据类
@Entity(tableName = "news")
data class NewsLocal(
@PrimaryKey
var id: Int = Random().nextInt(),
var publishedAt: String? = null,
var author: String? = null,
var urlToImage: String? = null,
var description: String? = null,
var source: Source? = null,
var title: String? = null,
var url: String? = null,
var content: String? = null
)
数据类External
data class Headlines(
val status: String? = null,
val publishedAt: String? = null,
val author: String? = null,
val urlToImage: String? = null,
val description: String? = null,
val source: NewsSource? = null,
val title: String? = null,
val url: String? = null,
val content: String? = null
)
data class NewsSource(
val name: String? = null,
val id: String? = null
)
我试图添加一些像下面,但不知道如何准确地Map
fun NewsRemote.toLocal() = NewsLocal(
..............
)
1条答案
按热度按时间sqxo8psd1#
标记实体或POJO的字段以允许嵌套字段(即注解字段类的字段),以便在SQL查询中直接引用。
如果容器是实体,这些子字段将是实体数据库表中的列。
源类新增嵌入实体。
您的实体
Map