Cancelling a Job, doesn't cancel the call
Closed this issue · 1 comments
Maikals commented
Hi,
I'm trying to do a search field that everytime a letter is added, the coroutine job is cancelled and a new job born to do the new query to the service.
The thing is that when I cancel the Job, if there's some problem with the service call, the Exception is not managed by anyone, so, the app crashes.
I am building the coroutine like this:
fun executeAsync(params: Params, block: suspend CoroutineScope.(T) -> Unit,
blockError: (CustomException?) -> Unit) {
job = launchAsync {
val result = buildRepoCall(params)
launchUI { block(result) }
}
job?.invokeOnCompletion {
if (it != null) blockError(CustomException(it)) //The exception is managed here.
}
}
and this are the coroutine wrappers:
/**
* Launch a coroutine in a new Thread.
* @param block The block that will be executed inside coroutine
*/
private fun launchAsync(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.async(Dispatchers.Default) {
block()
}
/**
* Launch a coroutine in the main thread.
* @param block The block that will be executed inside coroutine
*/
private fun launchUI(block: suspend CoroutineScope.() -> Unit): Job =
GlobalScope.launch(Dispatchers.Main) {
block()
}
The buildRepoCall launches the retrofit call. At the end of the path, this functions does the call:
override suspend fun getSearchMoviesList(searchText: String, page: Int): MovieListEntity =
MovieListMapper.toDomainObject(searchMoviesService
.createService().getSearchMovies(searchText, page).await())
Here I am doing the await to receive the data and transform it to a Domain Entity.
Thank you!
JakeWharton commented
Coroutine support is being moved into Retrofit directly where cancelation will be supported!