igor.wojda
02/14/2019, 4:00 PMJobCancellationException
and I am not sure what is the reason. My only hypothesis is that I am starting and canceling multiple times function that is in suspended state 🤔 but honestly I am not sure… Can somebody throw more light on this? What is the reason for throwing this exception?louiscad
02/14/2019, 5:57 PMcoroutineScope { ... }
for structured concurrency?igor.wojda
02/15/2019, 10:19 AMval job:Job? = null
fun getQuote() {
job?.cancel()
job CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
delay(300)
val result = repository.getQuote()
}
}
class Repository(val retrofitQuoteService:RetrofitQuoteService) {
suspend fun getQuote() {
retrofitQuoteService.getQuoteAsync().await()
}
}
getQuote
can be called multiple times as this is called while user types in text input - called each time text input changeslouiscad
02/15/2019, 11:03 AMgetQuote()
suspend function throws if there's a network issue or an unsuccessful response received. You need to catch it at call site (here, a `try`/`catch` in your launch
block).louiscad
02/15/2019, 11:04 AMigor.wojda
02/15/2019, 11:21 AMgetQuote
function is called multiple times (I sniff underlying network requests and they are correct), so I try to understand the reason behind it.
Also JobCancellationException
is as internal class so I cannot catch this exception explicitly. I need to get to core of this and understand why this happens.louiscad
02/15/2019, 11:26 AMJobCancellationException
is always the sign of an uncaught exception, usually happening in a launch
or async
block.bdawg.io
02/15/2019, 4:26 PMJobCancellationException
is thrown when you cancel the job via job.cancel()
bdawg.io
02/15/2019, 4:29 PMdelay
inside of your launch
block will throw that exception if the job is cancelled before your function resumes to your repository