Advitiay Anand
04/14/2022, 9:37 AMDazai
04/14/2022, 10:36 AMjava.lang.NegativeArraySizeException: -1537060379
at <http://io.ktor.utils.io|io.ktor.utils.io>.core.StringsKt.readBytes(Strings.kt:165)
at <http://io.ktor.utils.io|io.ktor.utils.io>.core.StringsKt.readBytes$default(Strings.kt:162)
at io.ktor.client.call.SavedCallKt.save(SavedCall.kt:73)
at io.ktor.client.call.SavedCallKt$save$1.invokeSuspend(SavedCall.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Tower Guidev2
04/14/2022, 11:58 AMGlobalScope
is there a Best Practice approach?Remon Shehata
04/14/2022, 12:06 PMDazai
04/15/2022, 10:55 AMMBegemot
04/15/2022, 1:59 PMExerosis
04/15/2022, 5:09 PMursus
04/18/2022, 12:25 AMwhile(true) {
yield() <--- In practise it seems yield emulates <http://Handler.post|Handler.post>
emit(Unit)
}
Or should I just wrap the ValueAnimator as Flow and call it a day?ursus
04/18/2022, 1:13 AMprivate var running = true
scope.launch {
flow {
var counter = 0
while (true) {
Log.d("Default", "waiting...")
while (running) {
delay(200)
emit(counter++)
}
}
}
.collect {
Log.d("Default", "value=$it")
}
}
fun pause() {
running = false
}
fun resume() {
running = true
}
how would I best implement pausing/resuming, without spamming “waiting…” when running == false
? i.e make it suspend and not retry the while condition so fast?Vlad
04/18/2022, 1:04 PMcoroutines-test:1.5
to 1.6.1 and can’t wrap my head around some of the new behaviours.
In 1.6 we seemingly have lost the ability to control the dispatch of individual dispatchers used internally by the classes under test, since all of the test dispatchers have to use a shared TestCoroutineScheduler
that now is the owner of the shared virtual time.
More details in the thread.ursus
04/18/2022, 10:01 PMfun ProgressThingy(duration: Long, running: Flow<Boolean>): Flow<Float> {
return callbackFlow {
val animator = ValueAnimator.ofFloat(0F, 1F)
animator.duration = duration
animator.addUpdateListener {
trySend(it.animatedFraction)
}
animator.addListener(onCancel = {
cancel()
})
animator.start()
running.collect {
if (it) animator.resume() else animator.pause()
}
awaitClose {
animator.cancel()
}
}
}
does anyone see a problem with collecting the running signal like that within the callback flow scope? it appears to work finesimon.vergauwen
04/19/2022, 10:26 AMFlow#flatMapMerge
or any of the other concurrent Flow
operator swallows CancellationException
.
Two examples in the thread.William Reed
04/19/2022, 6:37 PMCoroutineScope
which has a long lifecycle. I want to make another scope which has a shorter lifecycle (potentially several in the same span as the 'parent'). If the parent scope is cancelled I want both parent and the shorter lifecycle scope to be cancelled
as far as i can tell you can't inherit scopes like this. another (seemingly bad) idea I have is to maintain a collection of `Job`s for the shorter lifecycle group, which will allow me to cancel everything in that collection, or if the parent scope is cancelled, they would all be launched under it anyway, and get cancelledRemon Shehata
04/20/2022, 4:28 PMfun foo(){ // do something}
coroutineScope.launch{ foo }
2 - calling suspend function from a coroutine scope
suspend fun foo(){ // do something}
coroutineScope.launch{ foo }
julian
04/21/2022, 12:32 AMCoroutineExceptionHandler
installed on a top-level/root launch
coroutine builder will catch uncaught exceptions. That's what I found with coroutines 1.6.1
. The handler must be installed on the CoroutineScope
itself.
Just want to sanity check that this is actually the case, not something weird that I'm doing and not realizing.
Here's sample code of what used to catch, but seems to no longer:
fun main() {
val coroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, exception ->
println("Handle $exception in CoroutineExceptionHandler")
}
val topLevelScope = CoroutineScope(Job())
topLevelScope.launch {
launch(coroutineExceptionHandler) {
throw RuntimeException("RuntimeException in nested coroutine")
}
}
Thread.sleep(100)
}
Ayfri
04/21/2022, 12:52 PMcah
04/21/2022, 3:38 PMfun CoroutineScope.launchIO(block: suspend CoroutineScope.() -> Unit): Job {
return launch(
context = IO,
start = CoroutineStart.DEFAULT,
block = block
)
}
If I try to use this in place of the regular launch I get the following error:
java.lang.NoSuchMethodError: 'kotlinx.coroutines.Job com.some.misc.ExtensionsKt.launchIO(kotlinx.coroutines.CoroutineScope, kotlin.jvm.functions.Function2)'
I am more curious as to what I'm missing here rather than this being a serious use caseChris Fillmore
04/21/2022, 4:16 PMsuspend fun sendAndWait(message: Message): Message = withTimeout(5000) {
launch {
send(message)
}
// 'incoming' is a Flow<Message>
incoming.first { /* logic here which matches outbound messages with inbound ones by some matching id */ }
}
The timeout never works. It never times out, I don’t get an exception. There are two suspension points: send(message)
and incoming.first { }
. Is there something I’m misunderstanding about timeouts? withTimeout
has otherwise worked for me; I’m not sure what’s different in this case. Any ideas?Ridwan Farouki
04/23/2022, 10:03 PMCoroutineScope
to use a `TestScope`'s scheduler in kotlinx-coroutines-test:1.6.1
for the virtual time behaviour? I would have expected CoroutineScope(Job() + testScheduler)
to work but it doesn't. (Note that CoroutineScope(Job() + coroutineContext)
does work but then when I try to cancel differentScope
it fails the test because it cancels the parent TestScope
, making it hard to test code that uses structured concurrency)
fun tickTest() = runTest {
val differentScope = CoroutineScope(Job() + testScheduler)
val ticks = mutableListOf<Int>()
val tickJob = differentScope.launch {
var time = 0
while (true) {
delay(1_000)
ticks.add(++time)
}
}
advanceTimeBy(5_001)
tickJob.cancel()
println(ticks)
assert(ticks.size == 5)
}
Grouvie
04/24/2022, 5:16 PMColton Idle
04/26/2022, 8:39 PMfun doThing() {
viewModelScope.launch {
repository.getDocument()
...
suspend fun getDocument() {
return FirebaseFirestore.getInstance()
.collection("mycollec")
.document("mydoc")
.get()
.await()
.toObject<MyDoc>()
}
Any idea why the stacktrace I get doesn't point to the code above at all?Trey
04/27/2022, 2:16 PMTrey
04/27/2022, 5:14 PMTong Zhang
04/28/2022, 3:09 AMjames
04/28/2022, 5:05 AMrunBlocking {
val error = IllegalAccessException("Bad things are happening!")
val message = "Isn't it a nice day"
Span.current()?.let { span ->
val attributes = Attributes.builder().apply {
message?.let { put("error_message", it) }
}
.build()
span.setStatus(StatusCode.ERROR)
span.recordException(throwable, attributes)
}
launch {
// This will cause the spans to get linked together and then sync metadata from the above.
@WithSpan
suspendFunction()
}
}
The reason i'm asking is that i'm trying to understand some strange behavior in our system where the context doesn't always propagate from a parent to child correctly.Colton Idle
04/28/2022, 3:57 PMviewModelScope.launch {
repository.getBooks().collect { list ->
state.books.clear()
state.books.addAll(list)
}
repository.doSomething() <--- Will never get called
}
I almost want to write a lint check or something to prevent myself from shooting myself in the foot. Anyone have any suggestions on how to change my mindset on this?ildar.i [Android]
04/29/2022, 8:18 AMfirstFlow.combine(secondFlow) { text: String, role: Role ->
repository.getProcessList(role.apiParam, text)//returns new flow each time
}.flatMapLatest {
it
}
Tower Guidev2
04/29/2022, 10:59 AMisActive
as an approach to "join in with cancellation", however isActive
Nowak
04/29/2022, 1:34 PMAidan Low
04/29/2022, 2:07 PMtry {
foo()
} catch (ex: Exception) {
// ignore failures, best effort
}
but if I'm inside a coroutine, will this screw up cooperative cancellation? Do I need to do something like
try {
foo()
} catch (ex: CancellationException) {
throw ex
} catch (ex: Exception) {
// ignore failures, best effort
}
?Aidan Low
04/29/2022, 2:07 PMtry {
foo()
} catch (ex: Exception) {
// ignore failures, best effort
}
but if I'm inside a coroutine, will this screw up cooperative cancellation? Do I need to do something like
try {
foo()
} catch (ex: CancellationException) {
throw ex
} catch (ex: Exception) {
// ignore failures, best effort
}
?Tijl
04/29/2022, 2:09 PMAidan Low
04/29/2022, 2:28 PMephemient
04/29/2022, 3:43 PMAidan Low
04/29/2022, 5:15 PMthat also screws up Java thread interruptionWill that be handled by re-throwing CancellationException as in my second block of code?
ephemient
04/29/2022, 5:26 PMAidan Low
04/29/2022, 5:28 PM