question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RealmChangeListener not called for RealmList of RealmObject

See original GitHub issue

Goal

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 a CreateActivity that starts CreateIntentService to add or remove items from a RealmList (then, CreateActivity is finished)
  • when the MainActivity starts again, it adds a RealmChangeListener to the RealmList 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>
  • 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:closed
  • Created 6 years ago
  • Comments:40 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
Zhuindencommented, Apr 9, 2018

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.

2reactions
cmelchiorcommented, Nov 7, 2017

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.

11-07 10:55:27.625 13292-13292/com.jpmcosta.test.realmtestproject I/REALM_JAVA: MainActivity: onStart()
11-07 10:55:27.625 13292-13292/com.jpmcosta.test.realmtestproject I/REALM_JAVA: Size of list: 10
11-07 10:55:27.663 13292-13292/com.jpmcosta.test.realmtestproject I/REALM_JAVA: onChange() called on Realm. Size: 0
<!-- The List specific  Listener should have been triggered here
11-07 10:55:27.759 13292-13292/com.jpmcosta.test.realmtestproject I/REALM_JAVA: getItemCount(): 0

Incidentally, converting the RealmList to RealmResults have the same error, but they use the same underlying mechanics so it isn’t entirely unsurprising.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RealmChangeListener does not get called when Realm gets ...
I tried attaching rcl's directly to Realm , RealmResults and RealmObject , nothing works. The App has for example simple rcl's for RealmResults< ......
Read more >
RealmObject (Realm 10.3.1) - MongoDB
Adds a change listener to this RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the...
Read more >
Create reactive mobile apps in a fraction of the time - Realm
public class Person extends RealmObject { @Required // Name is not nullable ... So DO NOT use anonymous RealmChangeListener when calling addListener ....
Read more >
Sneak Peek: what's going to be new in Realm-Java 2.4.0 (or ...
BREAKING CHANGE: RealmChangeListeners added to RealmObject are now ... Previously, because RealmList is an actual table and not a result set ...
Read more >
Realm List's New Superpowers - Realm Academy
Realm's List type has gotten some new superpowers and it's time we gave them ... No fear! I'm glad to report that 3.0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found