I am experimenting with Kotlin Coroutines in my current Android application.
I have a use case where a user can search for text using a remote RestFul API.
What I would like to achieve is as follows:-
1). The use types "ABC" and I start my Remote API within this search string
viewModelScope.launch {
repository.searchAPI(searchString)
}
2). The user now types more so that my search string is now "ABCXYZ"
I now wish to cancel the initial search of "ABC" and replace it with a the new search string of "ABCXYZ"
I thought I could use this code...
viewModelScope.launch {
if (isActive) {
this.coroutineContext.cancelChildren()
}
repository.searchAPI(searchString)
}
However this cancels the entire process
How can I achieve the desired result of replacing a currently executing search with the most recent search string?