Mark
07/05/2023, 8:00 AMrunBlocking {
withTimeoutOrNull(100) {
runInterruptible(<http://Dispatchers.IO|Dispatchers.IO>) {
telephonyManager.networkCountryIso
}
}
}Rob
07/05/2023, 1:29 PMrunBlocking blocks the current thread. If that is the ui thread this will cause an ANR. To not block the ui thread you'll want to use a coroutine builder. Recommended scope to launch a coroutine in is in a lifecycle owner of a ViewModel, Activity, Fragment, View or Process depending on use case.Mark
07/05/2023, 1:32 PMrunBlocking will block the current thread until the passed code block has returned which, in this case, cannot last more than 100ms which is well under ANR threshold on Android. Why doesn’t the combination of withTimeoutOrNull and runInterruptible give the desired results?Rob
07/05/2023, 1:35 PMRob
07/05/2023, 1:36 PMMark
07/05/2023, 1:38 PMrunInterruptible does not make java blocking code actually interruptible by withTimeout ?Rob
07/05/2023, 1:40 PMThread.sleep(10000) inside withTimeout or runInterruptable it will still run to completion.Mark
07/05/2023, 1:44 PMrunInterruptible block is run to completion. AFAICT, runInterriptible will anyway throw a cancellation exception when the timeout is reached. and so that should result in the runBlocking completing as intended.Rob
07/05/2023, 1:44 PMsuspendCoroutine block. I think it will allow the outer coroutine to cancel.Rob
07/05/2023, 1:47 PMThread.sleep(10_000) and it does get interrupted. Sorry I can't help.Rob
07/05/2023, 1:49 PMrunBlocking on the main thread should prevent the ANR I think.Mark
07/05/2023, 1:50 PMThread.sleep). Yeah, it’s just common code that’s often called from non-suspending code.Mark
07/05/2023, 1:51 PMjdk.internal.misc.Unsafe.park (Native method)
java.util.concurrent.locks.LockSupport.parkNanos (LockSupport.java:234)
kotlinx.coroutines.BuildersKt.runBlocking$default (Builders.kt:1)Mark
07/05/2023, 2:00 PMwaiting ). Most of those are SQLDelight queries. So I wonder it’s an issue with running out of threads.Rob
07/05/2023, 2:02 PMRobert Williams
07/14/2023, 9:27 AMRobert Williams
07/14/2023, 9:27 AMRobert Williams
07/14/2023, 9:29 AMRobert Williams
07/14/2023, 9:30 AMMark
07/25/2023, 9:29 AMRobert Williams
07/25/2023, 11:34 AMRobert Williams
07/25/2023, 11:34 AMfor (i in 1..10000) {
BigInteger("$i").nextProbablePrime()
}
(takes about 1.5 seconds on kotlin playground)