是否有类似于此的内容:@Insert(onConflict = OnConflictStrategy.REPLACE),可在Room数据库中找到,您可以在Objectbox中执行此操作。如果没有,您如何避免在对象框实体中保存重复的条目?感谢。
答案 0 :(得分:0)
private Box<User> userBox;
userBox = ObjectBox.get().boxFor(User.class);
//your inserted object
if (!userBox.getAll().contains(insertedObject)){
leadBoxForLeadOpions.put(l);
}
答案 1 :(得分:0)
替换对象应该足够简单,只需将@id(assignable=true)
注释设置为所需的id字段即可。我已经通过以下方式在我的android应用中对其进行了测试:
@Test
fun testAssignableIdsGetReplaced() {
val myUser1 = User()
myUser1.id = 1
myUser1.nickname = "Original"
var currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 0)
boxStore!!.boxFor(User::class.java).put(myUser1)
currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 1)
assert(currentList.get(0).id == 1L)
val newUser1 = User()
newUser1.id = 1
newUser1.nickname = "New"
boxStore!!.boxFor(User::class.java).put(newUser1)
currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 1)
assert(currentList.get(0).id == 1L)
assert(currentList.get(0).nickname == newUser1.nickname)
}
@Entity
class User() {
@Id(assignable = true) var id: Long = 0
lateinitvar nickname: String
}
但是请记住,使用ids可分配的注释可能会产生副作用:
https://docs.objectbox.io/relations#updating-relations
https://docs.objectbox.io/advanced/object-ids#self-assigned-object-ids