我的应用程序目前正在使用 retrofit 和 Room,但我很难找到一种方法来自动生成 Db 对象的 id。当将 retrofit 对象映射到 RoomDb 实体时,即使我添加了自动生成密钥,它仍会不断要求输入 id 进行归档
以下是改造对象:
data class RetrofitProduct(
@SerializedName("id")
val id: String,
@SerializedName("types")
val types: List<String>,
@SerializedName("date")
val date: String,
)
以及相关的房间实体:
@Entity
data class ProductEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
val id: Int,
@ColumnInfo(name = "productId")
val productId: String,
@ColumnInfo(name = "types")
val types: List<String>,
)
Mapper如下:
fun RetrofitProduct.toEntity(): ProductEntity {
return ProductEntity(
productId = this.id,
types = this.types,
)
}
但是在构建时,编译器抱怨我需要添加id
:
参数“id”未传递任何值
我希望 Room 能够生成此 id,以便获取分页的项目数量。
基本上,如果我拉出最后一个项目,我知道其 id 例如是:25。所以我需要请求 26 及更多。