https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Louis Gautier

10/27/2023, 12:03 PM
Hi. I have a question about Flow. I have a function in my ViewModel that fire an http call. I’d like to refresh the data when the activity attached to the viewModel goes in the onResume callback - so re-executing an http call. Atm the http call is not executed on the second request
Copy code
fun callService() { 
    viewModelScope.launch {
        repository.getData().collect {
            // Update state. Called only once :(
        }
    }
}
s

streetsofboston

10/27/2023, 12:53 PM
Looks like (from the name) that getData() is a one-shot function, it returns one value. The getData() should be a plain suspend fun (not return a Flow) and the result of getData() should then be assigned to another Flow.
stateFlow.update { oldState -> calcState(oldState, repository.getData()) }
l

Louis Gautier

10/27/2023, 2:05 PM
Yeah I just figured out ! Make sens tho. Thanks
👍 1