android - 如何使用 Android Room 实体仅更新某些列?
问题描述
不幸的是,@Update 注释仅具有解决冲突形式的有限功能。我的实体包含远程、不可变或本地的数据。因此,当从远程源更新实体时,我需要保留或忽略对某些列的更新。
我想构建一个从远程源获取对象的更新,并且只更新某些列。
我的实体如下所示:
@Entity
public class MyExample {
@PrimaryKey
public String remoteId;
private String first;
private String second;
// This property is local only, don't want it reset.
private String third;
// constructor, getter and setter
}
这就是我现在正在做的事情:
@Query("UPDATE my_example SET first = :first, second = :second, WHERE remoteId = :remoteId")
protected abstract int updateRemote(String remoteId, String first, String second);
请注意,“第三”没有更新。
但是,每次我想添加一个新实体时,从头开始编写是非常乏味和耗时的。
我想能够做什么:
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
或者
@Query("UPDATE my_example SET first, second VALUES(entity) WHERE remoteId = :entity.remoteId") protected abstract int updateRemote(MyExample entity);
或者
@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);
或者
@Update(columns={"first", "second"})
protected abstract int updateRemote(MyExample entity);
其中任何一个都意味着我没有方法中每个参数的名称,我可以像普通@Update一样传递实体。
为了使这一点更好,将能够注释仅在@Insert 或@Update 期间设置的列。
有谁知道这样做的方法,而无需指定每个 inout 参数?
解决方案
推荐阅读
- python - 如何重新采样和插值(三次样条)时间序列数据
- javascript - 反应原生:图标不与原生库一起显示
- emscripten - C++ 到 webassembly 使用 Emscripten 运行并在 Deno 中运行
- amazon-web-services - 使用 AWS lambda 的异步后台作业
- python - Networkx 加权图悬停
- node.js - 在同一台服务器上运行 PHP 和 Node.js 的问题
- python - 为什么在归并排序算法中需要递归
- python - 如何获得单击事件的响应以使用 Python 处理 Selenium 上的异常?
- rsocket - RSocket-JS + RSocketResumableTransport 不工作
- java - 获取 https://localhost/myapp/saml/sso 重定向会引发 404 错误 - Spring MVC/Okta