是否有类似的东西:@Insert(onConflict = OnConflictStrategy.REPLACE) 可以在 Objectbox 中执行的 Room 数据库中找到。如果没有,您如何避免在对象框实体中保存重复条目?谢谢。
问问题
584 次
2 回答
1
在对象框 DataModel 类中,对项目进行注释
@unique 公共字符串用户 ID;
private Box<User> userBox;
userBox = ObjectBox.get().boxFor(User.class);
//your inserted object
if (!userBox.getAll().contains(insertedObject)){
leadBoxForLeadOpions.put(l);
}
于 2019-05-27T07:04:42.873 回答
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-身份证
于 2020-06-11T14:56:31.270 回答