darkmoon_uk
08/11/2019, 9:50 PM4.10
which is generating an incompatibly old version of meta-data vs. say, `4.8`+ which I'm having to use to support Java 12. In the event, I can compile ok, but multi-platform modules which are dependent on coroutines fail when publishing their own metadata.Shakil Karim
08/11/2019, 10:10 PMgiso
08/12/2019, 9:05 AMRainer Schlonvoigt
08/12/2019, 1:20 PM540grunkspin
08/12/2019, 1:58 PMDominaezzz
08/12/2019, 4:34 PMFlow.conflate()
allows emitter to never suspend but is there an operator to allow the collector to never suspend (too)?reline
08/12/2019, 7:45 PMtasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
kotlinOptions.freeCompilerArgs += ["-Xuse-experimental=kotlin.Experimental"]
}
}
zak.taccardi
08/12/2019, 9:28 PM1.2.1
and 1.3.0-RC2` that we need to be worried about? Especially for `@ExperimentalApi`s?zak.taccardi
08/12/2019, 9:30 PMraniejade
08/13/2019, 4:42 AMsuspend operator fun getValue/setValue
)coder82
08/13/2019, 10:31 AMcoder82
08/13/2019, 10:32 AMDias
08/13/2019, 1:37 PMDias
08/13/2019, 1:38 PMJan Skrasek
08/13/2019, 1:42 PMahulyk
08/13/2019, 2:09 PMursus
08/13/2019, 3:43 PMSeri
08/13/2019, 5:33 PMmolikuner
08/13/2019, 6:36 PMfun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
What I would expect:
suspend fun produceSquares(): ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
Later in the document you can find:
All functions that create coroutines are defined as extensions on CoroutineScope, so that we can rely on structured concurrency to make sure that we don't have lingering global coroutines in our application.But doesn't the
suspend
keyword does exactly that? It makes the current CoroutineContext implicitly available to the called function?voben
08/13/2019, 8:17 PMviewModelScope
. When the viewmodel gets destroyed before the long running operation finishes, the function println(“Hello World”) is still getting called. Wouldn’t the coroutine get cancelled and not print “Hello World” due to structured concurrency?
viewModelScope.launch {
doLongRunningOperation()
}
suspend fun doLongRunningOperation() = withContext(Dispatchers.Default) {
longRunningCalculation() // Not a suspending function
println("Hello World")
}
zjuhasz
08/14/2019, 12:55 AMChannel
or Flow
based API for my library and I’m having a hard time. I’m looking for some info / opinions / suggestions. I posted on the forum with all the info: https://discuss.kotlinlang.org/t/deciding-between-flows-and-channels/13858Tuan Kiet
08/14/2019, 7:45 AMcoder82
08/14/2019, 8:10 AMwinchester044
08/14/2019, 10:55 AMclass Foo {
val dispatcher// An android handler thread as a Coroutine Dispatcher
val job // Parent Job
val coroutineScope = job + dispatcher
fun <T> run(block: () -> T) {
// some prechecks
return runBlocking(coroutinScope.coroutineContext) {
// this runs on dispatcher
}
}
fun shutdown() {
// stop underlying runBlocking
job.cancel()
}
}
Consider the handler thread is busy doing some other stuff and runBlocking
is waiting for it to finish. Meanwhile shutdown()
gets invoked. Is there a way to 'cancel/stop' this runBlocking
call? (Without using thread interrupts, although once lambda has started execution cancellation becomes cooperative).Eric Martori
08/14/2019, 11:55 AMsuspend fun <T> Flow<T>.first() = take(1).single()
In the flow API?coder82
08/14/2019, 3:40 PMmolikuner
08/14/2019, 6:49 PMfun complete(): X {
TODO("your implementation")
}
suspend fun suspendComplete(): X {
return suspendCoroutine { continuation ->
val result = runCatching { complete() }
continuation.resumeWith(result)
}
}
Marko Mitic
08/15/2019, 10:44 AMtjohnn
08/15/2019, 10:53 AMnickheitz
08/15/2019, 11:59 AMimport kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.broadcast
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Test
import kotlin.coroutines.CoroutineContext
class BroadcastChannelTest : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = <http://Dispatchers.IO|Dispatchers.IO> + Job()
init {
System.setProperty("kotlinx.coroutines.debug", "")
}
@Test
fun broadcast() {
val jobs = mutableListOf<Job>()
val channel = Channel<Int>().broadcast(5, CoroutineStart.DEFAULT)
runBlocking {
jobs.add(createConsumer(channel))
jobs.add(createConsumer(channel, 500, 3))
delay(250)
(0..10).forEach {
channel.send(it)
delay(200)
}
channel.send(-1)
jobs.forEach {
it.join()
}
}
}
private fun createConsumer(channel: BroadcastChannel<Int>, delay: Int = 0, failOn: Int = 11): Job {
return launch {
try {
if (delay > 0) delay(delay.toLong())
println("creating consumer $delay")
val sub = channel.openSubscription()
for (msg in sub) {
if (isActive) {
println("$msg on ${Thread.currentThread().name}")
if (msg == -1) break
if (msg == failOn) {
sub.cancel()
throw Exception("Failing now...")
}
} else {
println("No longer active.")
}
}
} finally {
println("Consumer done")
}
}
}
}
in short, I want one of the channels to error out after 3 messages. GIven that all of these coroutines are (I think) running in the CoroutineScope of the class itself, the Job() at the top should lead to cooperative cancellation on an exception. However, the non erroring listener continues to completion. Any ideas?nickheitz
08/15/2019, 11:59 AMimport kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.broadcast
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Test
import kotlin.coroutines.CoroutineContext
class BroadcastChannelTest : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = <http://Dispatchers.IO|Dispatchers.IO> + Job()
init {
System.setProperty("kotlinx.coroutines.debug", "")
}
@Test
fun broadcast() {
val jobs = mutableListOf<Job>()
val channel = Channel<Int>().broadcast(5, CoroutineStart.DEFAULT)
runBlocking {
jobs.add(createConsumer(channel))
jobs.add(createConsumer(channel, 500, 3))
delay(250)
(0..10).forEach {
channel.send(it)
delay(200)
}
channel.send(-1)
jobs.forEach {
it.join()
}
}
}
private fun createConsumer(channel: BroadcastChannel<Int>, delay: Int = 0, failOn: Int = 11): Job {
return launch {
try {
if (delay > 0) delay(delay.toLong())
println("creating consumer $delay")
val sub = channel.openSubscription()
for (msg in sub) {
if (isActive) {
println("$msg on ${Thread.currentThread().name}")
if (msg == -1) break
if (msg == failOn) {
sub.cancel()
throw Exception("Failing now...")
}
} else {
println("No longer active.")
}
}
} finally {
println("Consumer done")
}
}
}
}
in short, I want one of the channels to error out after 3 messages. GIven that all of these coroutines are (I think) running in the CoroutineScope of the class itself, the Job() at the top should lead to cooperative cancellation on an exception. However, the non erroring listener continues to completion. Any ideas?kingsley
08/15/2019, 12:08 PMcoroutineContext
wrong, which might be a source of the issue
You should have a:
private val job = Job()
// and then
override val coroutineContext: CoroutineContext
get() = <http://Dispatchers.IO|Dispatchers.IO> + job
// OR
override val coroutineContext = <http://Dispatchers.IO|Dispatchers.IO> + Job()
<http://Dispatchers.IO|Dispatchers.IO> + Job()
is being passed down each timenickheitz
08/15/2019, 7:54 PM