RealmObject changeListener

11,553

Right now it is happening because our change detection is not granular enough. It will trigger change listeners for all objects of the same type, not just those that changed.

Getting the changelisteners to only notify if the exact object has changed is being tracked here https://github.com/realm/realm-java/issues/989.

Share:
11,553
juanes666
Author by

juanes666

Updated on August 07, 2022

Comments

  • juanes666
    juanes666 over 1 year

    I'm trying to understand notification types in Realm from the Notifications section in the official docs, and when I'm using RealmObject addChangeListener in multiple managed object all of them are called when only one object is changing.

    This is my code

    Person first = realm.where(Person.class).equalTo("id", 0).findFirst();
    
    first.addChangeListener(new RealmChangeListener<Person>() {
        @Override
        public void onChange(Person person) {
            Log.e(LOG_TAG, "First element is changing: " + person);
        }
    });
    
    Person second = realm.where(Person.class).equalTo("id", 1).findFirst();
    
    second.addChangeListener(new RealmChangeListener<Person>() {
        @Override
        public void onChange(Person person) {
            Log.e(LOG_TAG, "Second person is changing: " + person);
        }
    });
    

    When I trigger an update in any of these Person objects (for example in first) both of the listeners are being called.

    This what official docs say:

    Listeners can also be attached to RealmObject instances as well as RealmResults instances. This allows you to react to changes to your objects and query results.

    And

    Lastly, typed-based change listeners will get notified when their referenced types change.

    From what I understand the seen behaviour agrees with the second definition but I need to use the first behaviour, that's, I want to be notified when the object corresponding to that listener is changed. So, if first Person is updated, only its corresponding listener get notified, not all Person listeners.