backlinks

Defines a backlink that represents a one-to-one inverse relationship between an EmbeddedRealmObject and a TypedRealmObject.

You cannot directly modify the backlink itself, it must be done on the pointing object. Usage example:

class Town : EmbeddedRealmObject {
val county: County by backlinks(County::towns)
}

class County : RealmObject {
val towns: RealmList<Town> = realmListOf()
}

Because an EmbeddedRealmObject class might be referenced by different TypedRealmObject than the defined by the backlinks. In such cases an exception would be thrown stating that the pointing object could not might not be able to resolve into a T value.

Return

delegate for the backlinks collection.

Parameters

T

type of object that references the model.

sourceProperty

property that references the model.

Throws

if the backlink is not of type T


Returns a BacklinksDelegate that represents the inverse relationship between two an EmbeddedRealmObject and a TypedRealmObject.

Reified convenience wrapper for EmbeddedRealmObject.backlinks.


fun <T : TypedRealmObject> RealmObject.backlinks(sourceProperty: KProperty1<T, *>, sourceClass: KClass<T>): BacklinksDelegate<T>

Defines a collection of backlinks that represents the inverse relationship between two Realm models. Any direct relationship, one-to-one or one-to-many, can be reversed by backlinks.

You cannot directly add or remove items from a backlinks collection. The collection automatically updates itself when relationships are changed.

Backlinks for one-to-one relationships can be created on RealmObject properties:

class Town {
var county: County? = null
}

class County {
val towns: RealmResults<Town> by backlinks(Town::county)
}

Backlinks for one-to-many relationships can be created on RealmList, RealmSet or RealmDictionary properties:

class Parent : RealmObject {
var childrenList: RealmList<Child> = realmListOf()
var childrenSet: RealmSet<Child> = realmSetOf()
var childrenDictionary: RealmSet<Child?> = realmDictionaryOf() // Nullability of Child? is required by RealmDictionary
}

class Child : RealmObject {
val parentsFromList: RealmResults<Parent> by backlinks(Parent::childrenList)
val parentsFromSet: RealmResults<Parent> by backlinks(Parent::childrenSet)
val parentsFromDictionary: RealmResults<Parent> by backlinks(Parent::childrenDictionary)
}

Querying inverse relationship is like querying any RealmResults. This means that an inverse relationship cannot be null but it can be empty (length is 0). It is possible to query fields in the class containing the backlinks field. This is equivalent to link queries.

Because Realm lists allow duplicate elements, backlinks might contain duplicate references when the target property is a Realm list and contains multiple references to the same object.

Return

delegate for the backlinks collection.

Parameters

T

type of object that references the model.

sourceProperty

property that references the model.


inline fun <T : TypedRealmObject> RealmObject.backlinks(sourceProperty: KProperty1<T, *>): BacklinksDelegate<T>

Returns a BacklinksDelegate that represents the inverse relationship between two Realm models.

Reified convenience wrapper for RealmObject.backlinks.