realm-java: Object changelisteners does not trigger if a linked object changes
Goal
Detect changes of nested objects of a single RealmObject using RealmChangeListener .
Understand the differences between RealmChangeListener on RealmObject and RealmResults.
Expected Results
RealmChangeListener of a single RealmObject would listen to changes of its nested objects.
RealmChangeListener of a single RealmObject would listen to more changes than the RealmChangeListener of RealmResults.
Actual Results
RealmChangeListener of a single RealmObject doesn’t listen to changes of its nested objects.
RealmChangeListener of RealmResults listens to changes of its children’s nested objects.
Steps & Code to Reproduce
I created a simple Android project to reproduce my use case: https://github.com/jpmcosta/RealmTestProject/tree/262ee9099b47852c2b26bc2f7e3874c8670e6264
Schema:
Appobject contains aRealmColorproperty. That’s just a (bad) pattern to illustrate the issue.
How to reproduce the issue:
- there’s a list with a single
Appobject - click on that object to randomly change the
valueof itsRealmColorproperty - when
RealmColor.valuechanges we want to:- change the
Apptext color - change the FAB color
- change the
To change the App text color we add RealmChangeListener to RealmResults<App> and call notifyDatasetChanged when a change is detected.
To change the FAB color we add a RealmChangeListener to the App object and update its color when a change is detected.
Issue:
- The FAB color is not updated properly.
Version of Realm and tooling
Realm version: 3.3.2
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 11
- Comments: 39 (18 by maintainers)
Commits related to this issue
- Test project for https://github.com/realm/realm-java/issues/4786 — committed to jpmcosta/RealmTestProject by jpmcosta 7 years ago
@Zhuinden yes, that would work. I understand that
Appitself has not changed. But then, why does theRealmChangeListenerinRealmResults<App>detect a change? Also, if I have 10 nested objects, should I need to add 10 different listeners?You probably were updating the parent object in the same transaction, without really thinking about it.
My ugly solution is to update the parent object “manually”. Usually, I have an
updatedAt: Longproperty in the parent, which the child object is responsible to update whenever it is updated.Any solution I have the same problem, there is object class with field RealmList, listener on query of the highest hierarchy object with findAllAsync, but not register changes on the the field RealmList, and the results of the query, when I get RealmList from the object is very messed. With query with findAll is worked correctly.
I’ve come along this and want to get notified if a nested object changes (so I can update the UI showing the nested object).
I had the listener attached to
RealmObject(just like in the question here) which does not work (as mentioned) but when usingfindAllAsync()to get aRealmResultit doesn’t get notified either.So no luck for me using this workaround 😢. I’m using Realm
2.0.14.3.0.Has this changed lately? How can I get notified about changed on nested objects?
Is it related to the use of
findAllAsync()instead of the normalfindAll()? I’ve linked the async-result to be aLiveDataso it can be used with aViewModel(which works nice except of this nested-listener-problem).I believe we have an issue somewhere for listening to specific fields, including linked fields. That sounds like something similar to this. I’ll try to find it.
Thanks for your help, but I’m still not sure about using a query to keep a deep listener on the first item of a list. That would require to requery every time the first item changes (it’s common), and I’m not sure about the implications on performance. I still think it would be best to have a parameter to specify whether or not the listener should detect nested objects changes, but that would require some changes to realm-java. Thanks for the answer. 🙂
Edit: if you don’t think it’s worth discussing
RealmChangeListenerbehavior at the current time, feel free to close the issue, as my use case is already solved. 👍Well as you said, you could also refer to your single object with a
RealmResultsand then you’ll get nested link notifications, as specified in that comment I linked.Of course, I personally don’t know why this is how the notification behavior works.