Skip to content

Support data observers for related entities #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jpmcosta opened this issue Nov 21, 2017 · 11 comments
Open

Support data observers for related entities #268

jpmcosta opened this issue Nov 21, 2017 · 11 comments
Labels
enhancement New feature or request

Comments

@jpmcosta
Copy link

jpmcosta commented Nov 21, 2017

Note these related feature requests:


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
Copy link
Author

jpmcosta commented Nov 21, 2017

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

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

@greenrobot
Copy link
Member

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

@greenrobot greenrobot added the enhancement New feature or request label Nov 22, 2017
@mecoFarid
Copy link

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
Copy link

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

Seems like abondened issue

@greenrobot-team
Copy link
Member

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

@mecoFarid
Copy link

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

@greenrobot-team
Copy link
Member

@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
Copy link
Member

Please vote for #59

@greenrobot-team greenrobot-team added duplicate This issue or pull request already exists and removed enhancement New feature or request labels Sep 8, 2020
@greenrobot-team
Copy link
Member

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

@greenrobot-team greenrobot-team added enhancement New feature or request and removed duplicate This issue or pull request already exists labels Sep 8, 2020
@greenrobot-team greenrobot-team changed the title Support data observers for nested changes Support data observers for related entities Mar 22, 2022
@BobFactory
Copy link

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.

@greenrobot-team
Copy link
Member

@BobFactory No updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants