objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Support data observers for related entities

Open jpmcosta opened this issue 6 years ago • 11 comments

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>
}

jpmcosta avatar Nov 21 '17 12:11 jpmcosta

I think the following code could also trigger changes in data observers:

item.notes.add(Note())
item.notes.applyChangesToDb()

jpmcosta avatar Nov 21 '17 16:11 jpmcosta

I thought we have tracked that already, but did not find it...

greenrobot avatar Nov 22 '17 10:11 greenrobot

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

mecoFarid avatar Aug 17 '20 08:08 mecoFarid

I thought we have tracked that already, but did not find it...

Seems like abondened issue

mecoFarid avatar Aug 17 '20 08:08 mecoFarid

@mecoFarid If you are interested in this feature, please thumbs up the first post!

greenrobot-team avatar Aug 17 '20 08:08 greenrobot-team

@greenrobot-team Could be an unrelated place to ask but is there any workaround other than applyChangesToDb() which throws exception for untracted changes?

mecoFarid avatar Aug 17 '20 08:08 mecoFarid

@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.
}

greenrobot-team avatar Aug 17 '20 11:08 greenrobot-team

Please vote for #59

greenrobot-team avatar Sep 08 '20 13:09 greenrobot-team

Hm, actually might want to keep this separate. Reopening.

greenrobot-team avatar Sep 08 '20 14:09 greenrobot-team

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 avatar Dec 09 '22 05:12 BobFactory

@BobFactory No updates.

greenrobot-team avatar Dec 12 '22 07:12 greenrobot-team