realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
886 stars 52 forks source link

Backlinks relationship is not triggering flow update #1653

Closed FelipeRRM closed 4 months ago

FelipeRRM commented 4 months ago

How frequently does the bug occur?

Always

Description

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

Example:

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 {
    @PrimaryKey
    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>()
        .asFlow()
        .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 }

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

1.13.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android

Build environment

No response

nirinchev commented 4 months ago

As mentioned in the forums, this is working as designed. Backlink properties are computed and are a provided as a shorthand to doing the query itself. However, they're not considered when calculating changes, so updating a City will not trigger notifications on State since there's no direct link from State to City.

FelipeRRM commented 4 months ago

As mentioned in the forums, this is working as designed. Backlink properties are computed and are a provided as a shorthand to doing the query itself. However, they're not considered when calculating changes, so updating a City will not trigger notifications on State since there's no direct link from State to City.

Since I added cities to the keyPath keyPath = listOf("cities"), it's no longer the default null value, therefore I assume I lose the default behavior "changes to all top-level properties and nested properties 4 levels down will trigger a change.". Or do I still get that? Would be a pain to have to add all properties of an object in the keyPath (in this case, the name), since I want all changes to the model to trigger an update.

nirinchev commented 4 months ago

Yeah, unfortunately, adding the keypath will make you lose out on the default behavior. You should be able to use wildcards to avoid having to manually add the properties - e.g. "*" will get you all top-level properties, "*.*" will get you 2 levels deep, etc.