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

Bug: Flow doesn't emit new value when compound have relation to list of entities #12

Closed
antailyaqwer opened this issue Apr 18, 2024 · 2 comments · Fixed by #15
Closed
Assignees
Labels
bug Something isn't working

Comments

@antailyaqwer
Copy link
Contributor

Problem

When we have complex compound hierarchy with relation to a list of entities (or compounds) - reactive subscription via flow doesn't emit new values on changed entities.

Environment: kabin: 0.1.0-alpha05; android target.
SQLite version (according to documentation): 3.39 (API 34).

Example

data class Compound1(
    val entity: Entity1,
    
    @Relation(
        entity = Entity2::class,
        parentColumn = "id",
        entityColumn = "id"
    )
    val compounds2: List<Compound2>
)

@Dao
interface IEntity2Dao {

    @Query("DELETE FROM Entity2 WHERE id = :id")
    suspend fun removeEntity(id: String)
}

@Dao
interface IEntity1Dao {

    @Query("SELECT * FROM Entity1")
    suspend fun getCompound(id: String): Flow<Compound1>
}

to reproduce:

suspend fun reproducer(
    dao1: IEntity1Dao,
    dao2: IEntity2Dao
) {
    coroutineScope.launch {
        dao1.getCompound("id1").collect {
            // only first emit
        }

        dao2.removeEntity("id2")
    }
}
@tamimattafi
Copy link
Owner

The problem is that getCompound relies on queryEntity1 under the hood, which queries Entity1 without adding listeners on Compound2's entities.

We need to generate separate queries for compound entities, such as queryEntity1ForCompound1, which does register all child entities as listenable keys.

@tamimattafi tamimattafi self-assigned this Apr 18, 2024
@tamimattafi tamimattafi added the bug Something isn't working label Apr 18, 2024
@tamimattafi
Copy link
Owner

tamimattafi commented Apr 19, 2024

As I mentioned earlier, some changes have to be made with the naming to distinguish simple entity queries, and queries meant for compounds. The naming now looks like this:

  1. A simple entity query for Entity1 will be queryEntity1ByParameters
  2. A query for Entity1 that's inside Compound1 will be queryEntity1ForCompound1ByParameters

Changed here: 216c4e7

Making these changes to the current code of the compiler made the logic even more complex. Refactoring is required in order to maintain flexibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants