abbic
04/07/2023, 2:32 PMsuspend fun getBalanceFlow(): Flow<Double> {
val session = sessionDao.getCurrentSession() // suspend fun
return balanceDao.getBalanceFlow(session.userId)
}
in the viewmodel, we cant write
val balance = getBalanceFlow().asLivedata()
because getBalanceFlow must be a suspend function! any way around this?Casey Brooks
04/07/2023, 2:37 PMgetBalanceFlow()
should not be marked with suspend
. You should try to rewrite that function so that the initial getCurrentSession()
call happens within the flow, instead of being needed to create the flow.
This can usually be done pretty easily by creating a flow{ }
and using emitAll
with the upstream flow:
ffun getBalanceFlow(): Flow<Double> {
return flow {
val session = sessionDao.getCurrentSession() // suspend fun
emitAll(balanceDao.getBalanceFlow(session.userId))
}
}
abbic
04/07/2023, 2:38 PMabbic
04/07/2023, 2:38 PMCasey Brooks
04/07/2023, 2:41 PMabbic
04/07/2023, 2:45 PMgetCurrentSession()
contains code wrapped in withContext(<http://Dispatchers.IO|Dispatchers.IO>)
, that would be a problem?abbic
04/07/2023, 2:47 PMCasey Brooks
04/07/2023, 2:47 PMCasey Brooks
04/07/2023, 2:47 PMgetCurrentSession()
does not emit anythingCasey Brooks
04/07/2023, 2:48 PMwithContext()
abbic
04/07/2023, 2:48 PMabbic
04/07/2023, 2:48 PMCasey Brooks
04/07/2023, 2:49 PM