Colton Idle
08/07/2022, 5:14 PMfetchMoreData()
fun fetchMoreData(i) {
viewModelScope.launch { ...
but now if I call fetchMoreData() again, I actually want to cancel the old data fetching. How would I do that?ephemient
08/07/2022, 5:18 PMChrimaeon
08/07/2022, 5:20 PMlaunch
and cancel it before launching.ephemient
08/07/2022, 5:20 PMvar fetchJob: Job? = null
fun fetch() {
fetchJob?.cancel()
fetchJob = viewModelScope.launch { ... }
Colton Idle
08/07/2022, 5:22 PMephemient
08/07/2022, 5:24 PMval fetchRequests = MutableSharedFlow<Unit>()
fun start() {
fetchRequests.mapLatest { ... }.launchIn(viewModelScope)
}
fun fetch() {
fetchRequests.tryEmit(Unit)
}
Colton Idle
08/07/2022, 5:24 PMFrancesc
08/08/2022, 5:14 AMIf you need to launch a coroutine that keeps running after your function returns, then make your function an extension ofor passCoroutineScope
as parameter to make your intent clear in your function signature. Do not make these functions suspending:scope: CoroutineScope
https://elizarov.medium.com/coroutine-context-and-scope-c8b255d59055
Colton Idle
08/08/2022, 6:11 AM