Colton Idle
03/08/2022, 12:43 AMvar queryText = MutableStateFlow("")
init {
viewModelScope.launch {
myState.queryText.debounce(250).collect {
search(it)
}
}
}
private fun search(term: String) {
viewModelScope.launch {
val result = service.searchApiCall(term)
...
Now that I think about it. I probably want my old network calls to be cancelled too. Can anyone point me in the right direction about how I should rethink this?Kamesh Yadav
03/08/2022, 1:55 AMcollectLatest
will cancel the previous callColton Idle
03/08/2022, 1:57 AMKamesh Yadav
03/08/2022, 2:00 AMColton Idle
03/08/2022, 2:01 AMKamesh Yadav
03/08/2022, 2:45 AMChannelFlowTransformLatest
keeps the job reference for the previous block and only cancels that block, if the function is not suspend then it can not be cancelled, or if use use another launch then you have keep the job reference and cancel it yourselfColton Idle
03/08/2022, 2:48 AMinit {
viewModelScope.launch {
myState.queryText.debounce(250).collectLatest {
search(it)
}
}
}
private suspend fun search(term: String) {
val result = service.searchApiCall(term)
...
gildor
03/08/2022, 3:34 AMColton Idle
03/08/2022, 9:13 PMgildor
03/09/2022, 1:47 AMColton Idle
03/09/2022, 5:28 PM