https://kotlinlang.org logo
Title
b

brabo-hi

09/22/2021, 11:07 PM
Hi, i am using sqldelight to get data from database as flow, however the flow is not being collected whenever a new item is added
val players: Flow<List<HockeyPlayer>> = 
  playerQueries.selectAll()
    .asFlow()
    .mapToList()

players,collect{
   // i am not getting new list when item is added, i only receive data once
}
What could i be doing that is not right ?
r

Richard Gomez

09/23/2021, 12:31 AM
I suppose it depends on how SqlDelight has implemented the
asFlow()
function. Given that flows are 'cold', not 'hot' like channels, it's likely that the flow terminates once the first iteration of data is collected. https://elizarov.medium.com/cold-flows-hot-channels-d74769805f9
b

brabo-hi

09/23/2021, 12:35 AM
i am checking `
onCompletion {}
` but it never fires
t

Tiago Nunes

09/23/2021, 10:49 AM
Hi! Can you show the whole snippet where you’re calling players.collect?
If you are calling 2 terminal operator in the same coroutine, only the first one is ever called. Maybe that’s your case
I’m using SQLDelight and their FlowExtensions, everything works great
Another thing that could be happening is: you might be listening to the query in a different Database instance than you are updating it
b

brabo-hi

09/23/2021, 5:11 PM
@Tiago Nunes i am definitely reading and update from two different database instances, let me fix it to see
t

Tiago Nunes

09/23/2021, 5:35 PM
When you call asFlow, it registers a query listener in the database instance. When there are updates, all registered listeners in the database instance are notified. It needs to be the same db instance to trigger the flow
b

brabo-hi

09/23/2021, 5:49 PM
@Tiago Nunes thanks i just got it working, you are right, i was using different instances. Thanks a lot
👌 2