evan
01/18/2019, 8:54 PMGlobalScope.launch
, awaits a deferred value, prints its contents, and then outside of the launch method, prints a concluding String. All of this is working great. The process is still running when these two print tasks complete however. What is keeping it alive, and can I gracefully shutdown when all my work has been completed?
Edit: Updated code block from Louis' comments
suspend fun main() {
val api = createRestApi<RestApi>("<https://catfact.ninja/>", okHttpClient(), moshi)
coroutineScope {
val facts = api.getFacts().await()
println(facts.data)
}
println("That's all")
}
altavir
01/19/2019, 9:10 AMrunTest
and sadly, it does not solve the problem with "blocking" calls in JS (also it could be run only from test). I am still thinking if it is really necessary. My use case is that I have an interface with a regular blocking function and implementation which utilizes coroutines for parallel computations. Currently I can't do it since I can't collect parallel elements in a non-blocking way. Maybe there are better ways.otakusenpai
01/20/2019, 9:59 AMDmytro TOLSTYI
01/21/2019, 7:59 AMsuspend
function which I can't solve: I got function suspend fun one()
which executes some code and then calls a function that accepts a callback function as a param. But, this param function is not suspend. The issue is that I need to call another suspend fun from that callback param function. Here's an example:
suspend fun one() {
doSomething()
funWithNonSuspendCallback("data") {
two() // I need to call it from here but this is not suspend scope anymore
}
}
suspend fun two() {
}
ghedeon
01/21/2019, 12:47 PMfun main(){
runBlocking{
foo().consumeEach{ println(it) }
}
}
suspend fun foo(): ReceiveChannel<Int> = coroutineScope {
produce<Int>{
channel.send(42)
channel.close()
}
}
simon.vergauwen
01/21/2019, 1:12 PMDeferred<A>
? I was expecting to be able to define a handleErrorWith
operator that wraps an existing Deferred
to recover from errors but the program results in the original error.louiscad
01/21/2019, 3:05 PMDebugProbes.install()
but got this runtime exception: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;elizarov
01/21/2019, 8:55 PMselect { deferreds.forEach { it.onAwait { it } } }
elegant enough?ghedeon
01/21/2019, 11:45 PM@Test
fun test() {
runBlocking {
val channel = produce<Int> { channel.send(42) }
channel.consumeEach { assertEquals(42, it) } // <--- break point is on this lambda
}
}
P.S. I'm using Dispatchers.setMain()
on androidjudrummer
01/22/2019, 4:06 AMjudrummer
01/22/2019, 4:06 AMjudrummer
01/22/2019, 4:07 AMjudrummer
01/22/2019, 4:08 AMghedeon
01/22/2019, 10:40 AMamadeu01
01/22/2019, 2:44 PMDrew
01/22/2019, 4:39 PMrunBlocking
at all:
commonMain.dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.0'
}
androidMain.dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'
}
iosMain.dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.9.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.1.0'
}
Also, using Kotlin 1.3.11gumil
01/22/2019, 9:01 PMlaunch
? If I understand correctly launch
always run asynchronously and in tests we would like to run it sequentially.Allan Wang
01/23/2019, 4:27 AMsupervisorScope
and try catch it. However, it still doesn’t stop the cancellation of every other job, despite the main context already being a supervisorxyma
01/23/2019, 6:09 AM<http://Dispatchers.IO|Dispatchers.IO>
?julioyg
01/23/2019, 10:58 AMkotlinx-coroutines-debug
but we don't have that module listed in our gradle file 🤔bobby
01/23/2019, 11:30 AMspierce7
01/23/2019, 4:21 PMraulraja
01/24/2019, 2:53 PM@RestrictSuspension
annotations does a great job and has proven very useful for controlling side effects in Arrow. Kudos to everyone involved with the suspend system!
The only problem I'm having is that the compiler error messages for restricted contexts are not very useful. They just state the arbitrary suspended function can't run there.
I would like to customize such error messages a la carte to provide reasons why functions are restricted in a suspended receiver. For example:
save() can't be invoked in this [UI] scope because is scoped to [DB]
Is there a way to customize such error messages? If not is this something that would change in the compiler or std lib?Marc Knaup
01/25/2019, 9:56 AM@RestrictSuspension
! What is it good for? Is there an article about it? 😮irus
01/25/2019, 11:23 AMprivate val topics = mutableMapOf<String, Publisher>()
private val lock = Mutex()
suspend fun method(key: String) {
lock.withLock {
topics.getOrPut(key) { //... }
}
}
is lock gives guarantee that other coroutines will see changes in topics
?streetsofboston
01/25/2019, 5:55 PMlaunch
will use the CoroutineExceptionHandlerasync
, the exception is thrown when using await()
josephivie
01/25/2019, 9:03 PMConflatedBroadcastChannel
, correct?jossiwolf
01/26/2019, 12:11 PMsuspend fun
from Java, but I can't access any properties of Result
- when implementing Continuation
, I only get an Object
in resumeWith
. Can anybody help with that? 🙂Marc Knaup
01/27/2019, 8:18 PMAlexjok
01/28/2019, 7:56 AMoverride suspend fun save(something: Something) = withContext(Dispatchers.Default) {
doWork()
}
in the interface?Alexjok
01/28/2019, 7:56 AMoverride suspend fun save(something: Something) = withContext(Dispatchers.Default) {
doWork()
}
in the interface?gildor
01/28/2019, 7:58 AMinterface Savable {
suspend fun save(something: Something)
}
Alexjok
01/28/2019, 8:08 AMsave
method in jpa model
returns the saved object. I need a cup of coffee 🙂gildor
01/28/2019, 8:10 AM