objectbox-java
objectbox-java copied to clipboard
Support data observers for related entities
This might be a feature request. It seems that currently it is not possible to listen for nested changes.
- ObjectBox version: 1.2.1
- Reproducibility: always
boxFor(Item::class.java).query().build().subscribe().observer { items ->
// Not called when Note.text changes.
}
Entities
@Entity
class Item {
@Id
open var id: Long = 0L
lateinit open var notes: ToMany<Note>
}
@Entity
class Note {
@Id
open var id: Long = 0L
open var text: String? = null
@Backlink
lateinit open var parentItem: ToOne<Item>
}
I think the following code could also trigger changes in data observers:
item.notes.add(Note())
item.notes.applyChangesToDb()
I thought we have tracked that already, but did not find it...
applyChangesToDb
It will throw an exception if you try to call it on untracked changes. Like if it's first time you're putting that Note
into box
I thought we have tracked that already, but did not find it...
Seems like abondened issue
@mecoFarid If you are interested in this feature, please thumbs up the first post!
@greenrobot-team Could be an unrelated place to ask but is there any workaround other than applyChangesToDb()
which throws exception for untracted changes?
@mecoFarid Not sure what you mean, calling applyChangesToDb()
is not a workaround for the reported issue AFAIK.
A workaround to not getting notifications on related entities is to observe the box of the related entity as well. Using the above example:
boxFor(Note::class.java).query().build().subscribe().observer { items ->
// Called when Note.text changes.
}
Please vote for #59
Hm, actually might want to keep this separate. Reopening.
Hi, Is there any update on this issue? I have the following data
@Entity
data class Habit(
@Id var id: Long = 0L,
var name: String = "",
) {
@Backlink
lateinit var progress: ToMany<Progress>
}
@Entity
data class Progress(
@Id var id: Long = 0L,
@Convert(converter = LocalDateConverter::class, dbType = Date::class)
val preformDate: LocalDate = LocalDate.now(),
var performCount: Int = 0,
) {
lateinit var habit: ToOne<Habit>
}
I want to Observe the changes in both habit and progress. If there are changes to the progress object there should be a change event but at the moment it is not so. The change listener doesn't get triggered when there is a change in the nested entity of the habit. Even if I observe the Progress entity separately there should be a way to tell the habit entity to refresh its toMany collection.
Observing nested entities separately makes no sense in this case since the parent entity already encapsulates them both and we have attached a change listener to it. This seems similar approach to how the SQL databases do the job. But in SQL we could do this with a left join specifically.
Nevertheless, it feels a little unnatural to do this in a NoSQL Database.
@BobFactory No updates.