kotlinx.coroutines

Kotlin coroutine swallows exception

放肆的年华 提交于 2020-01-03 11:54:27
问题 I'm very confused about how Exception handling works with coroutines. I was hoping that it would be possible to have a chain of suspend functions that would pass Exceptions between themselves like synchronous code. So if say Retrofit threw an IOException, I could handle that exception at the beginning of the chain of suspend functions such as in a presenter to show an error to a user. I made this simple example to try out coroutines but if I uncomment either throw Exception call the code

Delay() accuracy issues / Weird behavior of job scheduler

本秂侑毒 提交于 2019-12-24 10:56:31
问题 I'm currently trying to build a job scheduler as shown below. My goal is to be able to schedule the launch of arbitrary functions (here of (Long) -> Unit)) with as much accuracy as possible on their launch time (sub-millisecond would be ideal). import java.util.* import kotlinx.coroutines.* import java.util.concurrent.PriorityBlockingQueue import kotlin.math.max import java.time.Instant fun nowInMicrosSinceEpoch() : Long { val now = Instant.now() return now.toEpochMilli() * 1000L + (now

Multithreading using Kotlin Coroutines

大兔子大兔子 提交于 2019-12-22 13:52:18
问题 I'm experimenting with Kotlin Coroutines and have following code: fun main(args: Array<String>) = runBlocking { val cores = Runtime.getRuntime().availableProcessors() println("number of cores: $cores") val jobs = List(10) { async(CommonPool) { delay(100) println("async #$it on thread ${Thread.currentThread().name}") } } jobs.forEach { it.join() } } This is my output: number of cores: 4 async number:0 on thread ForkJoinPool.commonPool-worker-2 async number:2 on thread ForkJoinPool.commonPool

Can't catch exception throwing after Kotlin coroutine is cancelled

一世执手 提交于 2019-12-22 10:49:36
问题 Using kotlinx.coroutines lib I can't catch an exception if it was thrown after coroutine is canceled. This leads to app crash. fun foo() { val job = launch(UI) { try { Log.d("TAG", "Start coroutine") run(CommonPool) { Log.d("TAG", "Start bg task") // Intentionally make bg task running for a long time SystemClock.sleep(2000) Log.d("TAG", "Throw bg task exception") throw RuntimeException("Bg task exception") } } catch (e: Exception) { Log.e("TAG", "Handle coroutine exception", e) } } launch(UI)

Method myLooper in android.os.Looper not mocked with Coroutines

非 Y 不嫁゛ 提交于 2019-12-22 08:48:15
问题 I want to do some test of coroutines in JUnit but I met some problems. Code is easy: @Test fun coroutineTest() { //runBlocking(Unconfined) doesnt work too runBlocking () { delay(1000) println("test") } } But I got that error java.lang.RuntimeException: Method myLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details. at android.os.Looper.myLooper(Looper.java) at kotlinx.coroutines.experimental.android.MainLooperChecker.checkRunBlocking(HandlerContext.kt

Kotlin Async vs Launch

对着背影说爱祢 提交于 2019-12-22 06:49:41
问题 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

Kotlin Coroutines with returning value

China☆狼群 提交于 2019-12-19 05:23:45
问题 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 } 回答1: To return exactly Int , you need to get out of the coroutine world and that's what runBlocking is for: fun

How to transform an Android Task to a Kotlin Deferred?

好久不见. 提交于 2019-12-18 15:55:08
问题 Firebase anonymous sign in returns a task (which is basically Google promise implementation): val task:Task<AuthResult> = FirebaseAuth.getInstance().signInAnonymously() How it would be possible create a signInAnonymous wrapper where: It is a suspend function, waiting for the task completion suspend fun signInAnonymous(): Unit It returns a Deferred object, delivering the result asynchronously fun signInAnonymous() : Deferred 回答1: Based on this GitHub library, here's a way to transform a Task

can I always use WorkManager instead of coroutines?

心不动则不痛 提交于 2019-12-13 12:08:47
问题 I wonder why should I bother with rx or coroutines when there is brilliant solution as WorkManager. But for almost all tutorials they use coroutines so may be WorkManager has disadvantages ? 回答1: The scope of both is different. WorkManager is to schedule deferrable (for any later time) or immediately. tasks asynchronously. As documentation says The WorkManager API makes it easy to specify deferrable, asynchronous tasks and when they should run. These APIs let you create a task and hand it off

Module with Main dispatcher is missing

回眸只為那壹抹淺笑 提交于 2019-12-12 08:18:00
问题 I'm trying to make a background call to my local database and update the UI with the results using coroutines. Here is my relevant code: import kotlinx.coroutines.experimental.* import kotlinx.coroutines.experimental.Dispatchers.IO import kotlinx.coroutines.experimental.Dispatchers.Main import kotlin.coroutines.experimental.CoroutineContext import kotlin.coroutines.experimental.suspendCoroutine class WarehousesViewModel(private val simRepository: SimRepository) : BaseReactViewModel