Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backlinks relationship is not triggering flow update #1653

Closed
FelipeRRM opened this issue Feb 6, 2024 · 3 comments
Closed

Backlinks relationship is not triggering flow update #1653

FelipeRRM opened this issue Feb 6, 2024 · 3 comments

Comments

@FelipeRRM
Copy link

FelipeRRM commented Feb 6, 2024

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
Copy link
Member

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.

@nirinchev nirinchev closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2024
@FelipeRRM
Copy link
Author

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
Copy link
Member

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.

@sync-by-unito sync-by-unito bot closed this as completed Feb 17, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants