kotlin-coroutines

Exception not being caught in Coroutines

只愿长相守 提交于 2020-02-22 03:01:07
问题 I can't seem to get my error-handling done in coroutines. I've been reading lots of articles and the exception handling documentation but I can't seem to get it working. Here's my setup: My ViewModel launches the coroutine with it's scope class MyViewModel(private var myUseCase: MyUseCase) : ViewModel() { private val viewModelJob = Job() private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) fun doSomething() { uiScope.launch { try { myUseCase() } catch (exception: Exception) {

When you throw an exception in a coroutine scope, is the coroutine scope reusable?

限于喜欢 提交于 2020-02-04 05:09:05
问题 I've been having problems figuring out error handling with coroutines that I've narrowed down to this unit test with the following steps: I create a coroutine scope, with any dispatcher. I throw an exception anywhere within this scope in an async block (or even in a nested async block). I call await on the returned deferred value and handle the exception. This is all fine. However, when I try to use the same coroutine scope to launch a new coroutine, this always completes exceptionally with

Spring Boot Rest Service With Kotlin Coroutines

旧街凉风 提交于 2020-01-25 07:02:25
问题 I am writing a Spring Boot Rest Service in Kotlin. I want to use coroutines as an alternative to Webflux to achieve non-blocking asynchronous behavior. I am using Spring Boot 2.1 and know that I cannot achieve true non-blocking behavior because I am blocking at Controller . However, I am okay with that for now till the time Spring Boot 2.2 is generally available. My app is three-layered ie Controller->Service->Repository. In repository, I am calling other services ie network calls and have

Kotlin to achieve multithread request hedging?

依然范特西╮ 提交于 2020-01-24 18:55:47
问题 Spring's reactor has an interesting feature : Hedging . It means spawning many requests and get the first returned result , and automatically clean other contexts. Josh Long recently has been actively promoting this feature. Googling Spring reactor hedging shows relative results. If anybody is curious , here is the sample code . In short , Flux.first() simplifies all the underlaying hassles , which is very impressive. I wonder how this can be achieved with Kotlin's coroutine and multithread ,

Kotlin to achieve multithread request hedging?

核能气质少年 提交于 2020-01-24 18:55:31
问题 Spring's reactor has an interesting feature : Hedging . It means spawning many requests and get the first returned result , and automatically clean other contexts. Josh Long recently has been actively promoting this feature. Googling Spring reactor hedging shows relative results. If anybody is curious , here is the sample code . In short , Flux.first() simplifies all the underlaying hassles , which is very impressive. I wonder how this can be achieved with Kotlin's coroutine and multithread ,

Safe async in a given scope

烈酒焚心 提交于 2020-01-24 09:49:08
问题 I want to start an async coroutine with a suspending function in a given parent CoroutineScope , to produce a Deferred that might then be used from any coroutine in that scope. I would like its job to be cancelled if the parent's job is cancelled, but if the suspending function throws an exception, I need that captured in the resulting Deferred without cancelling sibling jobs in the parent scope. The way I'm doing it works fine, but I'm wondering if there's a simpler, more idomatic way than

Safe async in a given scope

有些话、适合烂在心里 提交于 2020-01-24 09:49:07
问题 I want to start an async coroutine with a suspending function in a given parent CoroutineScope , to produce a Deferred that might then be used from any coroutine in that scope. I would like its job to be cancelled if the parent's job is cancelled, but if the suspending function throws an exception, I need that captured in the resulting Deferred without cancelling sibling jobs in the parent scope. The way I'm doing it works fine, but I'm wondering if there's a simpler, more idomatic way than

Kotlin Coroutines choosing Dispatcher

被刻印的时光 ゝ 提交于 2020-01-24 09:29:29
问题 I'm trying to figure out which Dispatcher to use in what case. I read some documentation and came up with this: Default → CPU work Main → main (update UI) Unconfined IO → writing file / network tasks Is this right? What about Unconfined? 回答1: You're right in your assumptions about the dispatcher types. Unconfined dispatcher – A coroutine dispatcher that is not confined to any specific thread Unconfined dispatcher is appropriate when coroutine does not consume CPU time nor updates any shared

Kotlin coroutine can't handle exception

旧城冷巷雨未停 提交于 2020-01-24 02:41:06
问题 I was playing around with coroutines and found some very strange behavior. I want to convert some asynchronous requests in my project using suspendCoroutine() . Here's piece of code showing this problem. In first case, when suspend function is being called in runBlocking coroutine, exception from continuation goes to catch block, and then runBlocking finishes successfully. But in second case, when creating new async coroutine, exception goes through catch block and crashes the whole program.

Difference between CoroutineScope and coroutineScope in Kotlin

跟風遠走 提交于 2020-01-24 01:07:29
问题 Can anyone give clarity between functions CoroutineScope() and coroutineScope() ? When I tried to check in source, I found that both of them are functions of CoroutineScope.kt . Additionally, coroutineScope() is suspend function while other one is normal function Below is documentation I could find : /** * Creates a [CoroutineScope] that wraps the given coroutine [context]. * * If the given [context] does not contain a [Job] element, then a default `Job()` is created. * This way, cancellation