mysqlkotlinkotlin-flowsqldelight

SqlDelight, MySql, and Flow: Flow's collect lambda not invoked on database changes


I'm playing around with SqlDelight using MySql with Hikary datasource in a plain JVM project using Kotlin and Flow to consume queries results. The first execution of a query returns the data as expected, however the Flow#emit() is not triggered when the data on the database changes.

This one is the repository function I'm invoking:

fun getAllUser(): Flow<List<User>> = database.userQueries.getAll()
      .asFlow()
      .mapToList()

This is the main function where the repository function is invoked:

    fun main() = runBlocking<Unit> {   repository.getAllUser().collect {
      println("Collecting users: $it")
   }
}

I've tried debugging the underling code and the listener on the Query object is registered and the main is correctly blocked on the execution of the repository function. Anyone else has experienced something similar?


Solution

  • So stupid, my bad! I was updating the database manually and expecting to see the Flow magically updating. Anyway, updating the database by means of SqlDelight functions causes the Flow to emit the updated recordset as expected.