我有以下实体:
@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
}
其中本地信息不存储在服务器上,仅存储在手机本地。
目前,当我从服务器提取信息时,GSON 自动填充我的值,但由于“本地”不是来自服务器,因此不会填充到该对象中。
有没有一种方法,当我调用更新时,我可以让 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
}
使用 @Ignore 注释,尝试忽略通用更新中的本地字符串。然后提供自定义更新语句以仅保存本地信息
@Query("UPDATE foo SET local = :newLocal WHERE foo.id = :id")
fun updateLocal(id: Long, newLocal: String)
然而,ROOM 似乎足够聪明,可以检查我是否在本地属性上使用了 @Ignore,并且它不会使用该更新语句进行编译。
有任何想法吗?