los*_*ion 5 android gson android-room
我有以下实体:
@Entity
class Foo(
@PrimaryKey
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "thing1")
val thing1: String,
@ColumnInfo(name = "thing2")
val thing2: String,
@ColumnInfo(name = "thing3")
val thing3: String,
@ColumnInfo(name = "thing4")
val thing4: String
) {
@ColumnInfo(name = "local")
var local: String? = null
}
Run Code Online (Sandbox Code Playgroud)
其中本地是不存储在服务器上的信息,只存储在手机本地。
目前,当我从服务器中提取信息时,GSON 会自动填充我的值,但由于“本地”不是来自服务器,因此不会填充到该对象中。
有没有一种方法,当我调用 update 时,我可以让 Room 跳过“本地”列的更新,而无需编写自定义更新以插入除“本地”以外的所有其他列?痛点是我可以有很多列,我添加的每个新列都必须添加到自定义插入语句中。
我还考虑过从服务器实体到新的“本地”实体的一对一映射,但是现在我必须处理连接语句的痛苦,因为我需要本地信息。
我希望我能做这样的事情:
@Entity
class Foo(
@PrimaryKey
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "thing1")
val instructions: String,
@ColumnInfo(name = "thing2")
val instructions: String,
@ColumnInfo(name = "thing3")
val instructions: String,
@ColumnInfo(name = "thing4")
val instructions: String
) {
@Ignore
var local: String? = null
}
Run Code Online (Sandbox Code Playgroud)
使用@Ignore 注释,尝试忽略通用更新上的本地字符串。然后提供一个自定义更新语句来保存本地信息
@Query("UPDATE foo SET local = :newLocal WHERE foo.id = :id")
fun updateLocal(id: Long, newLocal: String)
Run Code Online (Sandbox Code Playgroud)
但是 ROOM 似乎足够聪明,可以检查我是否在本地属性上使用了 @Ignore,并且它不会使用该更新语句进行编译。
有任何想法吗?
Raw*_*awa 12
部分更新已添加到房间中2.2.0
在 Dao 中,您执行以下操作:
// Here you specify the target entity
@Update(entity = Foo::class)
fun update(partialFoo: PartialFoo)
Run Code Online (Sandbox Code Playgroud)
并沿着您的实体Foo创建一个PartialFoo包含主键和要更新的字段的实体。
@Entity
class PartialFoo {
@ColumnInfo(name = "id")
val id: Long,
@ColumnInfo(name = "thing1")
val instructions: String,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1834 次 |
| 最近记录: |