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

Sudhir Singh Khanger

08/10/2020, 10:53 AM
I am running a network call as follows. Repository
Copy code
fun home(): Flow<HomeResponse> =
        flow { emit(ApiService.home()) }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
ViewModel
Copy code
@ExperimentalCoroutinesApi
fun home() {
    viewModelScope.launch {
        repository.home()
            .onStart { _homeLiveData.postValue(Resource.loading(null)) }
            .catch { _homeLiveData.postValue(Resource.error(it.message ?: "", null)) }
            .collect { _homeLiveData.postValue(Resource.success(it)) }
    }
}
Is there anything built-in in Flow that would ignore the repeated calling of
home()
method and let the existing request fail or reach success?
w

wasyl

08/10/2020, 11:00 AM
I think SharedFlow is the closest to what you’d like, but it’s not released yet. I’m not sure it’d support your use case exactly, but I don’t think there’s anything built-in right now
s

Sudhir Singh Khanger

08/10/2020, 11:03 AM
Thanks. In that I case I will enable/disable the button once I start the asynchronous work and get up to desired state.
z

Zach Klippenstein (he/him) [MOD]

08/10/2020, 3:36 PM
`SharedFlow`/`StateFlow` could help here, but I think you’d still have another problem to solve. I’d suggest using a library like Dropbox’s Store (https://github.com/dropbox/Store) which handles this sort of debouncing for you.
3 Views