madsbf
02/03/2019, 10:10 AMsuspend fun select(contentMap: Map<Content, VideoContentItemHolder>) = coroutineScope {
select<Content> {
contentMap.forEach { content, itemHolder ->
launch { itemHolder.onClick() }.onJoin {
content
}
}
}
}
VideoContentItemHolder.onClick() is a suspending function. Basically I am waiting for any of the itemHolder.onClick() to complete, and then returning the associated Content-object. I have attached the debugger to see that onClick() successfully completes for one of the itemHolders and I enter the onJoin-block, but the select clause never gets a result. So anyone waiting for the result is suspended indefinitely.dave08
02/03/2019, 6:05 PMgetCompletedOrNull()
that returns a null instead of throwing... so currently the only way is to runCatching { deferred.getCompleted() }.getOrElse { defaultValue }
?trathschlag
02/04/2019, 9:00 AMDalinar
02/04/2019, 11:52 AMGlobalScope
in `GlobalScope.async { }`but without using coroutineScope { }
- I mean I want to write x.async{ }
- how do I do that? what should x
be? to make it the equivalant of doing coroutineScope { }
? is this possible? the reason I ask is because it requires a lot of reworking to use coroutineScope { }
for this section of codeDalinar
02/04/2019, 12:43 PMfun
? can the code in that fun
also be suspended?simon.vergauwen
02/04/2019, 1:25 PMstartCoroutineCancellable
?koufa
02/04/2019, 2:04 PMlaunch
coroutine? Is it only possible by using coroutineScope
and add a try catch around it?Bernhard
02/04/2019, 2:12 PMpardom
02/04/2019, 4:28 PMCoroutineDispatcher
(i.e. not Dispatchers.Main
) that enforces a calling thread? I want to launch some tasks that return to the calling thread.streetsofboston
02/04/2019, 6:10 PMReceiveChannel
that the caller can use to receive data sent from repeated callback.
If something goes wrong, the Channel
implementing the returned ReceiveChannel
is closed with a Throwable
.
How can I catch this throwable?
I can’t find a method on a ReceiveChannel
that takes a callback function to handle a side-effect of closing a channel (with or without an error).bdawg.io
02/05/2019, 3:09 AMaltavir
02/05/2019, 7:20 AMspierce7
02/05/2019, 6:14 PMSubject
with kotlinx.corotuines? There are too many edge cases with Channel
where I risk losing events, and having to deal with them all the time is exhausting.spierce7
02/05/2019, 7:21 PMBehaviorSubject
with a BroadcastChannel
? ConflatedBroadcastChannel
allows for the loss of events after subscription, while BehaviorSubject
guarantees all events to be delivered.
I've tried swapping to an ArrayBroadcastChannel
to help ensure all events are delivered, but I have no way to ensure that the current value is delivered to a new subscriber upon a new subscription being opened. Any suggestions?Dico
02/05/2019, 9:02 PMKulwinder Singh
02/06/2019, 7:38 AMfirstAction()
will be called even if i cancel job while coroutine is suspended by delay, what i'm doing wrong here ?adeln
02/06/2019, 11:02 AMzokipirlo
02/06/2019, 12:18 PMlaunch
a coroutine where I call one long blocking method (some library in native code) on <http://Dispatcher.IO|Dispatcher.IO>
, is it possible that this coroutine executes on a thread on which some other coroutine is running and will block execution of that other coroutine?streetsofboston
02/06/2019, 6:30 PMReceiveChannel
to get notified when it is closed, either through an error or without one (just normally closed)?mp
02/06/2019, 7:50 PMniqo01
02/07/2019, 12:01 AMlaunch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
delay(500)
println("Unconfined : After delay in thread ${Thread.currentThread().name}")
}
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking: I'm working in thread ${Thread.currentThread().name}")
delay(1000)
println("main runBlocking: After delay in thread ${Thread.currentThread().name}")
}
I get:
Unconfined : I'm working in thread main @coroutine#2
main runBlocking: I'm working in thread main @coroutine#3
Unconfined : After delay in thread kotlinx.coroutines.DefaultExecutor @coroutine#2
main runBlocking: After delay in thread main @coroutine#3
Instead of:
Unconfined : I'm working in thread main
main runBlocking: I'm working in thread main
Unconfined : After delay in thread kotlinx.coroutines.DefaultExecutor
main runBlocking: After delay in thread main
Any idea why ?ansman
02/07/2019, 2:25 AMsuspend fun performHeavyWork()
Would it be advisable to use an actor for this case? The callee should be able to receive a progress value between 0-1 (or 0-100).Nikky
02/07/2019, 2:25 AMEdouard Goossens
02/07/2019, 10:18 AMactual fun observe(): ReceiveChannel<DocumentSnapshot> {
val channel = Channel<DocumentSnapshot>(Channel.CONFLATED)
val removeListener = documentReference.onSnapshot({
channel.offer(DocumentSnapshot(it))
}, {
channel.close(it)
})
channel.invokeOnClose { removeListener() }
return channel
}
jkbbwr
02/07/2019, 10:56 AMwithoutclass
02/07/2019, 4:18 PMansman
02/07/2019, 6:12 PMval pendingJobs = Collections.synchronizedMap(mutableMapOf<String, Deferred<Stuff>>())
suspend fun getStuff(key: String): Stuff = pendingJobs.computeIfAbsent(key, ::computeStuff)
suspend fun computeStuff(key): Stuff = /* Long running operation */
but that doesn’t feel like it’s the Kotlin way.kevinherron
02/07/2019, 6:56 PMgroostav
02/08/2019, 12:07 AMMyWorkingThing: kotlinx.coroutines.Job
, what kinds of things should you do with cancellation? In my case, MyWorkingThing
acquires a resource that I would like to synchronize on the disposal of before letting the job hit the Compelted
state. The Cancelling
state seems to reflect this well: if you hit cancel()
on a job, it goes cancelling
while it attempts to clean up its resources.
The problem is cleaning up those resources is itself a long-running operation, and I cant suspend something while its being cancelled because, well, its being cancelled. Should I just block? Should I just document MyWorkingThing
with some docs to the effect of:
hey, if you cancel aits going to launch some teardown jobs in global scope.MyWorkingThing
Dias
02/08/2019, 3:31 PMDias
02/08/2019, 3:31 PMgroostav
02/10/2019, 12:29 AMkotlinx.coroutines
, its best to stay away from volatile
, java.util.concurrent
et al. I also suspect you're trying to use a deferred, which is fundamentally a one-shot value type, as the facade into a running process (which it is not).var deferred
, which is having its value changed on restarts, how about you use a Channel<String>
? a ConflatedBroadcastChannel
might do what you want it to do.Dias
02/11/2019, 9:28 AM