kotlinx.coroutines

CoroutineExceptionHandler not executed when provided as launch context

旧城冷巷雨未停 提交于 2019-12-06 06:41:19
问题 When I run this: fun f() = runBlocking { val eh = CoroutineExceptionHandler { _, e -> trace("exception handler: $e") } val j1 = launch(eh) { trace("launched") delay(1000) throw RuntimeException("error!") } trace("joining") j1.join() trace("after join") } f() This is output: [main @coroutine#1]: joining [main @coroutine#2]: launched java.lang.RuntimeException: error! at ExceptionHandling$f9$1$j1$1.invokeSuspend(ExceptionHandling.kts:164) at kotlin.coroutines.jvm.internal.BaseContinuationImpl

Kotlin coroutines: Switching context when testing an Android Presenter

江枫思渺然 提交于 2019-12-05 21:40:31
I've started using kotlin coroutines in my Android project recently, but I have somewhat of a problem with it. Many would call it a code smell. I'm using an MVP architecture where the coroutines are started in my presenter like this: // WorklistPresenter.kt ... override fun loadWorklist() { ... launchAsync { mViewModel.getWorklist() } ... The launchAsync function is implemented this way (in my BasePresenter class that my WorklistPresenter class extends): @Synchronized protected fun launchAsync(block: suspend CoroutineScope.() -> Unit): Job { return launch(UI) { block() } } The problem with

How to implement natural (aka. smart) batching with Kotlin channels?

自闭症网瘾萝莉.ら 提交于 2019-12-05 14:05:08
Natural aka. smart batching is a technique in stream processing that optimizes throughput without affecting latency. On the example of a concurrent queue, the consumer has the ability to atomically drain all the items observed at some instant and then process them as a batch. Ideally, the queue should be bounded, giving an upper limit to the batch size and providing backpressure to the sender at the same time. It's called "natural" batching because there's no imposed batch size: when the traffic is low, it will process each item as soon as it arrives. In that case you don't need any throughput

Kotlin Async vs Launch

女生的网名这么多〃 提交于 2019-12-05 11:24:34
Kotlin_version = '1.2.41' I am pretty new to Kotlin. I would like to know what's the difference between async and launch . Especially in the following code import kotlinx.coroutines.experimental.async import kotlinx.coroutines.experimental.awaitAll import kotlinx.coroutines.experimental.delay import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.runBlocking fun main(args: Array<String>) { runBlocking { (20..30).forEach { launch{ println("main before" + it) val outer = it delay(1000L) val lists = (1..10) .map { async{anotherMethod(outer, it)}} println("main after-

What is the correct way of using Anko Coroutines extensions?

孤街浪徒 提交于 2019-12-05 07:15:34
问题 So I am migrating an example app from RxJava to Kotlin/Anko Corountines and I am wondering if I am doing the best (first) approach of it: fun getPopulationList() { val ref = asReference() async(UI) { try { ref().setCurrentState(ViewState.State.LOADING) val background = bg { repository.populationResponse().execute().body() } ref().let { it.response = background.await() it.mvpView?.onGetData(it.response) it.setCurrentState(ViewState.State.FINISH) } } catch (e: Exception) { e.printStackTrace()

CoroutineExceptionHandler not executed when provided as launch context

情到浓时终转凉″ 提交于 2019-12-04 12:05:09
When I run this: fun f() = runBlocking { val eh = CoroutineExceptionHandler { _, e -> trace("exception handler: $e") } val j1 = launch(eh) { trace("launched") delay(1000) throw RuntimeException("error!") } trace("joining") j1.join() trace("after join") } f() This is output: [main @coroutine#1]: joining [main @coroutine#2]: launched java.lang.RuntimeException: error! at ExceptionHandling$f9$1$j1$1.invokeSuspend(ExceptionHandling.kts:164) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32) at kotlinx.coroutines.ResumeModeKt.resumeMode(ResumeMode.kt:67)

Kotlin Android debounce

你。 提交于 2019-12-03 12:03:28
Is there any fancy way to implement debounce logic with Kotlin Android? I'm not using Rx in project. There is a way in Java , but it is too big as for me here. You can use kotlin coroutines to achieve that. Here is an example . Be aware that coroutines are experimental at kotlin 1.1+ and it may be changed in upcoming kotlin versions. UPDATE Since Kotlin 1.3 release, coroutines are now stable. Thanks to https://medium.com/@pro100svitlo/edittext-debounce-with-kotlin-coroutines-fd134d54f4e9 and https://stackoverflow.com/a/50007453/2914140 I wrote this code: private var textChangedJob: Job? = null

Kotlin Coroutines with returning value

寵の児 提交于 2019-12-01 03:33:35
I want to create a coroutine method which has returning value. For example) fun funA() = async(CommonPool) { return 1 } fun funB() = async(CommonPool) { return 2 } fun sum() { launch { val total = funA().await() + funB().await() } } If I want to return total in sum method, how should I do? like, fun sum(): Int { launch { val total = funA().await() + funB().await() } return total } To return exactly Int , you need to get out of the coroutine world and that's what runBlocking is for: fun sum(): Int = runBlocking { funA().await() + funB().await() } See Bridging blocking and non-blocking worlds in

How to implement timer with Kotlin coroutines

和自甴很熟 提交于 2019-11-30 21:23:42
I want to implement timer using Kotlin coroutines, something similar to this implemented with RxJava: Flowable.interval(0, 5, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .map { LocalDateTime.now() } .distinctUntilChanged { old, new -> old.minute == new.minute } .subscribe { setDateTime(it) } It will emit LocalDateTime every new minute. I believe it is still experimental, but you may use a TickerChannel to produce values every X millis: val tickerChannel = ticker(delayMillis = 60_000, initialDelayMillis = 0) repeat(10) { tickerChannel.receive() val currentTime = LocalDateTime

How to implement timer with Kotlin coroutines

痴心易碎 提交于 2019-11-30 17:43:49
问题 I want to implement timer using Kotlin coroutines, something similar to this implemented with RxJava: Flowable.interval(0, 5, TimeUnit.SECONDS) .observeOn(AndroidSchedulers.mainThread()) .map { LocalDateTime.now() } .distinctUntilChanged { old, new -> old.minute == new.minute } .subscribe { setDateTime(it) } It will emit LocalDateTime every new minute. 回答1: I believe it is still experimental, but you may use a TickerChannel to produce values every X millis: val tickerChannel = ticker