fal
03/19/2021, 12:45 AMprivate val refreshFlow =
MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST).apply {
tryEmit(Unit)
}
init {
fetchNews()
}
fun refreshData() {
refreshFlow.tryEmit(Unit)
}
private fun fetchNews() {
viewModelScope.launch {
refreshFlow
.flatMapLatest { fetchLatestNewsFlow() }
...
.collect()
}
Is this ok? Are there any better solutions?ephemient
03/19/2021, 12:52 AMFlow<Unit> coming from various places, .flattenMerge() them all together for a single stream of "somebody wants a refresh now" triggersfal
03/19/2021, 1:08 AMephemient
03/19/2021, 1:09 AMfal
03/19/2021, 1:11 AMbaxter
03/19/2021, 5:14 AMSingle or Maybe, consider using a suspend function or just launch a single use coroutine with launch or async on a scope.
• If you need a stream such as Observable or Flowable, and the stream would complete, use flow {}.
• If the stream is hot, then use MutableSharedFlow() or MutableStateFlow().baxter
03/19/2021, 5:16 AMfetchLatestNewsFlow() that is doing a single task (fetching the latest news and caching it). You can probably just call fetchLatestNewsFlow().launchIn(viewModelScope) each time you call refreshData().baxter
03/19/2021, 5:19 AMfetchLatestNewFlow(), unwrap it from the flow block, and make that function a suspend function, so I can just do:
fun refreshData() {
viewModelScope.launch { fetchLatestNews() }
}baxter
03/19/2021, 5:19 AMfal
03/19/2021, 10:35 AM