RealmChangeListener not called for RealmList of RealmObject
See original GitHub issueGoal
After adding a RealmChangeListener
to a RealmList
, if the number of its elements changes, the listener should be called.
Expected Results
RealmChangeListener
should always be called when the number of elements of a RealmList
changes.
Actual Results
RealmChangeListener
is not always called when the number of elements of a RealmList
changes.
Steps & Code to Reproduce
I created a simplified Android project to reproduce the issue’s workflow: https://github.com/jpmcosta/RealmTestProject/tree/9375194c255977ae944fde292b7ca5b9ba845f5a
The workflow of the issue:
MainActivity
opens aCreateActivity
that startsCreateIntentService
to add or remove items from aRealmList
(then,CreateActivity
is finished)- when the
MainActivity
starts again, it adds aRealmChangeListener
to theRealmList
and logs its current number of items - the listener’s
onChange()
method is not called, despite the number of items being changed (this is the issue)
To reproduce:
- run the app
- click on the floating action button to open
CreateActivity
and change the number of elements of the list- if the list is empty, it should fill the list with 10 items
- if the list has items, it should clear all items
- logs are created when (log_tag:
ItemListAdapter
)- the
RealmChangeListener
is added:add listener <current number of items>
- the
RealmChangeListener
is removed:remove listener
- the
RealmChangeListener.onChange()
:onChange() <current number of items>
- the
- to understand the issue:
- notice the number of items when the listener is added, and compare it to the list’s actual number of items
- notice whether or not
onChange()
was called (usually, it’s not called).
- you can also long press the floating action button to add a single item, to make sure the listener is working (it should work and a
onChange()
log should be created)
I’m reproducing the issue consistently, but not 100% of the time (maybe 99%).
If you can’t reproduce the issue, try to change the Don't keep activities
setting in Android’s Developer options.
Version of Realm and tooling
Realm version: 4.1.0
Android Studio version: 3.0
Which Android version and device: 7.1.2
Issue Analytics
- State:
- Created 6 years ago
- Comments:40 (22 by maintainers)
If you have custom item types in your Adapter then Realm doesn’t know about those. We ran into this by using
getItemViewType
for showing empty views, but that is not Realm bug ofc.Yes, but the problem is not the recreation of the adapter as far as I can see. What I’m seeing is that a
RealmListener
on the Realm is triggered due to the background write, but the more specialized listener on realmlist is not.This indicate that the problem is not around the adapter, but how the listeners work.
Incidentally, converting the
RealmList
toRealmResults
have the same error, but they use the same underlying mechanics so it isn’t entirely unsurprising.