Ahmed
02/13/2024, 1:34 PMval job = launch {
if (slugs.isNotEmpty()) {
model.locations = withTimeout(10.seconds) {
locationsRepository.getLocations(slugs)
}
}
}
// Do other tasks
job.join() // Wait for timeout or completed job
return@withContext model
The docs for withTimeout state that
Runs a given suspending block of code inside a coroutine with the specified timeout and throws aif the timeout was exceeded.TimeoutCancellationException
The code that is executing inside the block is cancelled on timeout and the active or next invocation of the cancellable suspending function inside the block throws aIt is apparent from the docs that my block would be cancelled and an exception is thrown. Would this exception crash the App or would it just cancel the job?.TimeoutCancellationException
ephemient
02/13/2024, 1:45 PMTimeoutCancellationException is a CancellationException so it will bubble up and cancel the containing scope as well. but it doesn't cancel the Job so you could catch it insideephemient
02/13/2024, 1:46 PMwithTimeoutOrNull than try-catchSam
02/13/2024, 1:47 PMAhmed
02/13/2024, 1:51 PMjob.join() will throw a CancellationException , which will in turn cancel the parent scope as well. Right?Sam
02/13/2024, 1:53 PMwithTimeout propagates to the job and causes it to terminate with a silent (non-failure-type) cancellation. That doesn't propagate, so the outer scope doesn't see any errors. The job.join() won't throw any exception, it'll just resume normally as soon as the timeout expires.Ahmed
02/13/2024, 1:54 PMSam
02/13/2024, 1:55 PM