如何使用Android Room实体仅更新某些列?

时间:2018-07-09 17:47:27

标签: android android-room android-architecture-components

不幸的是,@ 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);

OR

@Query(“ UPDATE my_example设置第一个,第二个VALUES(entity)WHERE remoteId =:entity.remoteId“)     受保护的抽象int updateRemote(MyExample实体);

OR

@Query("UPDATE my_example SET first = :entity.first, second = :entity.second, WHERE remoteId = :entity.remoteId")
protected abstract int updateRemote(MyExample entity);

OR

@Update(columns={"first", "second"})
protected abstract int updateRemote(MyExample entity);

其中任何一个都意味着我在方法中没有每个参数的名称,我可以像对普通@Update那样传递实体。

要使其变得更好,将能够注释仅在@Insert或@Update期间设置的列。

有人知道没有指定每个inout参数的方法吗?

0 个答案:

没有答案