Backlinks relationship is not triggering flow update: Kotlin SDK

Flows do not get re-emitted with the latest DB state when backlinked objects are added/removed.


I have a City Model:

class City : RealmObject {
    var name: String? = null
    var state: State? = null // by backlinks(State::towns)

And a State Model:

class State : RealmObject {
    var id: String = RealmUUID.random().toString()
    var name: String? = null
    val cities: RealmResults<City> by backlinks(City::state)

I have a query to get all the states in a flow, very simple:

private val states = realm.query<State>()
        .map { it.list }

The idea is that whenever a new city is added/removed/changed, the flow should get the latest values from the DB, with the updated state (since it’s list of cities changed, it should trigger a re-emission).

The problem is that this is not happening! Adding or removing a city does not trigger the states query flow to update.
The objects are being added/deleted successfully, because running the query again manually returns the most up to date version of the DB, it’s just that the flow itself is not representing the latest state of the DB.

I have managed to get it working by manually adding a keyPath to the flow builder, but that shouldn’t be necessary since, according to the documentation, “If no keypaths are provided, changes to all top-level properties and nested properties 4 levels down will trigger a change.”

private val states = realm.query<State>()
        .asFlow(keyPath = listOf("cities"))
        .map { it.list }

This is expected behavior - backlinks are not considered when calculating changes for an object as they’re a computed property.