objectbox / objectbox/objectbox-java
Entities in Backlink ToMany reappearing after removing
还没有人认领这个 Issue。
- 主要语言
- Java
- 星标
- 4.6k
- 派生
- 311
- PR 合并指标
- 30 天内没有已合并 PR
描述
Describe the bug
When trying to remove entities from a One-To-Many relationship, they keep reappearing in the database after put() when first removing from the relationship and afterwards from box
Basic info (please complete the following information):
- ObjectBox version: 3.1.0
- Reproducibility: always
- Device: Galaxy S10 (Android 11), OnePlus 6T (Android 11), Pixel 4 (Android 12)
- OS: Android 11 and 12
To Reproduce
Steps to reproduce the behavior:
- Create 2 entities with a 1:N relationship (
oneandmany) - Add some entities to the ToMany (
many) of the other (one). - Remove 1 entity from the relationship with
one.many.removeById(id) - Remove the entity from the box
box.remove(id) - Put the parent (
one) in the box again (overriding it)
Expected behavior
No matter the order, if an entity gets deleted from the box and the relation and is nowhere to be found, it should not reappear in the box.
Code
My Repo
Example code where I tested the issue in a Kotlin project
fun main(args: Array<String>) {
val store: BoxStore = MyObjectBox.builder().name("objectbox-notes-db").build()
val imageBox = store.boxFor<Image>().also { it.removeAll() }
val propertyBox = store.boxFor<Property>().also { it.removeAll() }
val originalImage = Image()
imageBox.put(originalImage)
originalImage.apply {
properties.addAll(listOf(
Property(name = "h1"),
Property(name = "h2"),
Property(name = "h3")
))
}
imageBox.put(originalImage)
println("Boxed Properties: " + propertyBox.all.map { it.id })
println("Original Image Properties: " + originalImage.properties.map { it.id })
val removedID = propertyBox.all.first().id
println("Removing id ${removedID}")
val image = imageBox.get(originalImage.id).also {
it.properties.removeById(removedID) //changing the order of these 2 will make
propertyBox.remove(propertyBox.all.first().id) // it work as expected
it.properties.applyChangesToDb()
}
println("--------------")
println("Both houldn't have ${removedID}: ")
println("Boxed Properties before put: " + propertyBox.all.map { it.id })
println("New Image Properties before put: " + image.properties.map { it.id })
imageBox.put(image)
println("--------------")
println("Should still not have ${removedID}")
println("Boxed Properties after put: " + propertyBox.all.map { it.id })
println("New Image Properties after put: " + image.properties.map { it.id })
}
@Entity
data class Image(
@Id
var id: Long = 0,
....
) {
@Backlink(to = "image")
lateinit var properties: ToMany<Property>
}
@Entity
data class Property(
@Id
var id: Long = 0,
..
) {
lateinit var image: ToOne<Image>
}
Additional context
Flipping the order (e.g. first removing the entity from the box) and THEN removing it from the ToMany works.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Kotlin 复现以及 app/src/main/java/com/mobilecomputing/collit/data/datasource/LocalImageDataSource.kt 开始,重点关注 ToMany.removeById、applyChangesToDb 和 Box.put。复现两种删除顺序,并跟踪关系更新。当已删除的 Property 在 put parent 后无论操作顺序如何都不再出现时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- android, kotlin
- 领域
- database, mobile
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100