0

I have an SQL query that selects all items where two of the fields are NULL.

My repository looks like this:

    override suspend fun flowTeamsWithNoGroupByGameId(gameId: Int): Flow<List<Team>> {
    return transactionQueries
            .selectAllTeamsWithNoDesignatedGroupByGameId(gameId.toLong())
            .asFlow()
            .mapToList()
            .map { queryResult ->
                queryResult.map {
                    Team(
                        teamId = it.id ?: "",
                        name = it.name ?: "",
                        federation = it.federationId ?: 0,
                        latitude = it.latitude ?: 0.0,
                        longitude = it.longitude ?: 0.0
                    )
                }
            }
}

My ViewModel:

    viewModelScope.launch(Dispatchers.IO) {
       launch {
          repository.flowTeamsWithNoGroupByGameId(TEMP_GAME_ID).collect {
             Log.d(LOG_TAG, "teams collected $it")
             _allTeams.value = it
          }
       }
    }

The code basically loads a list of teams. The teams are displayed when the screen is opened successfully. However, when an update is made on the database through the app, the list doesn't reflect the changes (I verified the table is updated).

The log shows that it doesn't collect the new values.

When I use the App Inspection tool to make changes on the database manually, the log shows that it collects the values.

So, I'm confused why is this happening?

Any ideas or explanation? I will appreciate it!

MS2099
  • 369
  • 2
  • 11
  • 1
    can you collect new value with `repository.flowTeamsWithNoGroupByGameId(TEMP_GAME_ID)` after updating the table? – Phil Dukhov Apr 26 '23 at 01:32
  • I thought I needed to call it once and as long as the coroutine is active that it would observe any changes in the database. Did I understand the concept wrong? – MS2099 Apr 26 '23 at 01:49
  • @PhilDukhov sorry, to answer your questions, yes. If I use that line after updating the dB, it does collect it, but still how come when I update a field in the db trough the App Inspection tool, I don't need that line of code. Why is that? – MS2099 Apr 26 '23 at 02:02
  • I just wanted to make sure there's no problem in the query itself. I haven't faced such problem. I'd try running it on `MainScope()` just to make sure your coroutine is not dead, if it's not - looks like a bug to me. You can try updating to **2.0.0-alpha05** - it contains a lot of bug fixes. Alpha here means that API is not stable - so you may need to update your code in newer releases, but in terms of performance it's stable. If your bug still reproduces there, I'd say you need to report it with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Phil Dukhov Apr 26 '23 at 02:13

1 Answers1

0

Remove suspend keyword from the functions returning flow Functions returning flow should not be suspended. Reason being suspend function will suspend the code block before your flow could get returned and you collect. So it would be in forever suspended state