Ananiya
04/14/2022, 3:58 AMsuspend fun get() = stateChannelFlow(viewModelScope){firebase.database./**some stuff*/.get.addOnSuccessListener { data ->
viewModelScope.launch {
send(data)
}
}
}
ik, you could say you should have used stateFlow instead ofstateChannelFlow I did but it forced me to use channel over regular flow
btw called this function in Fragment() lifecycleScope
at this time i have no idea if I messed the coroutine thing on get() but i tried my best trying d/n things which failed
N: this is the code for the extension function
suspend fun <T>stateChannelFlow(scope: CoroutineScope, @BuilderInference block: suspend ProducerScope<T>.() -> Unit): StateFlow<T> = channelFlow(block).stateIn(scope)
gildor
04/14/2022, 4:00 AMgildor
04/14/2022, 4:04 AMgildor
04/14/2022, 4:05 AMstojan
04/14/2022, 8:34 AMcallbackFlow
is the way... here is an example:
https://piotrprus.medium.com/use-kotlin-coroutines-for-firebase-real-time-database-streams-e28d6083709cgildor
04/14/2022, 9:33 AMAnaniya
04/14/2022, 3:51 PMstateFlow
which was quite nice
However it wasn't working as I wanted it
I was expecting it to update the UI with stream of data changeAnaniya
04/14/2022, 3:55 PMgildor
04/16/2022, 2:15 AMI was expecting it to update the UI with stream of data changeMaybe they have flow adapter? If they don’t you can write own with callbackFlow