kotlin-extensions / io.realm.kotlin / addChangeListener

addChangeListener

fun <E : RealmModel> E.addChangeListener(listener: RealmChangeListener<E>): Unit

Adds a change listener to a RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.

Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.

class MyActivity : Activity {

    private var person: Person?

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        person = realm.where<Person>().findFirst()
        person?.addChangeListener(RealmChangeListener { person ->
            // React to change
        })
    }
}

Parameters

listener - the change listener to be notified.

Exceptions

IllegalArgumentException - if the object is null or an unmanaged object, or the change listener is null.

IllegalStateException - if you try to add a listener from a non-Looper or IntentService thread.

IllegalStateException - if you try to add a listener inside a transaction.

fun <E : RealmModel> E.addChangeListener(listener: RealmObjectChangeListener<E>): Unit

Adds a change listener to a RealmObject to get detailed information about the changes. The listener will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.

Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.

class MyActivity : Activity {

    private var person: Person?

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        person = realm.where<Person>().findFirst()
        person?.addChangeListener(RealmObjectChangeListener { person, changeSet ->
            // React to change
        })
    }
}

Parameters

listener - the change listener to be notified.

Exceptions

IllegalArgumentException - if the object is null or an unmanaged object, or the change listener is null.

IllegalStateException - if you try to add a listener from a non-Looper or IntentService thread.

IllegalStateException - if you try to add a listener inside a transaction.