zak.taccardi
09/10/2020, 8:43 PMJob
, how can I fail it with an exception? I want to cancel it abnormally so its parent failstaer
09/10/2020, 9:21 PMemitAll
a flow that has the retry appended. Again, it's all working perfectly, but something seems just off about it.. Seems like too many flows.
Thanks
https://pl.kotl.in/ysrpoKNa7Allan Wang
09/11/2020, 8:12 AMsuspend fun main
, or do we have to convert it to fun main
and add runBlocking
?Ahmed Ibrahim
09/11/2020, 12:50 PMJob
?
suspend <T> fun executeQuery(query: T) {
val deferred = client.queryCall(query).toDeferred()
currentCoroutineContext()[Job]?.invokeOnCompletion {
if (it is CancellationException) {
deferred.cancel(it)
}
}
deferred.await()
}
voben
09/11/2020, 3:32 PMsend
suspend function would suspend the coroutine until receiver started listening but that doesn’t seem to be the case. Can anyone provide some input on how to make this work?
Sender class
private val _myEvents = BroadcastChannel<String>(1)
val myEvents = _viewEvents.asFlow()
...
fun doSomething() {
_myEvents.send("sample string")
}
Rita Curado
09/11/2020, 5:25 PMCould not find org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.9.
asad.awadia
09/11/2020, 6:35 PMBrendan Weinstein
09/13/2020, 5:19 AMShashank
09/13/2020, 5:57 PMStateFlow
by asserting the list of values received by it.
data class State(val isLoading: Boolean, val name: String?)
fun main() {
val mutableStateFlow = MutableStateFlow(State(false, null))
val stateFlow = mutableStateFlow as StateFlow<State>
val scope1 = CoroutineScope(Job())
val scope2 = CoroutineScope(Job())
scope1.launch {
delay(1000)
mutableStateFlow.value = State(true, null)
mutableStateFlow.value = State(false, "name")
}
scope2.launch {
stateFlow.collect {
println("value = $it")
}
}
Thread.sleep(2000)
}
Here is my code. Instead of getting State(true, null)
as the second value, I only receive the initial value and the last value (State(false, name)
)spierce7
09/13/2020, 8:34 PMFlow
to an Observable
safe? The reason I’m wondering is because the Observable
can be cancelled, but the Flow
can’t. Is the suspending behavior of the coroutine sufficient?
With Kotlin Multiplatform for the JS side we’re converting `Flow`s to rxjs
Observable
s. Code inside the thread.groostav
09/14/2020, 8:59 PMCoroutineDispatcher
into a coroutineContext such that we could ~automagically make progress bars based on the number of suspension points?
I think this is akin to reflecting on the number of states the coroutine state machine has. Of course, while-loops and if-statements blow this up, but still I feel like there might be something here.
Has anybody got an implementation doing something like this?Ricardo C.
09/15/2020, 9:50 AMSudhir Singh Khanger
09/15/2020, 10:45 AMDaniele Segato
09/15/2020, 1:13 PMshareIn
/ stateIn
before it is released?
Do i need to use a -SNAPSHOT
dependency? Which one?
thanks.Pritam Kadam
09/16/2020, 12:13 PMDispatchers.Default
or create separate scope with fixed thread pools equal to number of cores?
what are the pros and cons? what are the criteria's to choose between one of this approach? for ex. Library writer, App writer etc.
I have posted detailed explanation and my use cases here https://stackoverflow.com/questions/63915078/can-i-use-dispatchers-default-for-blocking-cpu-bound-operationsspierce7
09/16/2020, 6:15 PMDavide Giuseppe Farella
09/17/2020, 7:34 AMFlow
like
flow {
while (true) {
emit(something)
delay(interval)
}
}.flowOn(injectedDispatcher)
In my test, I inject the dispatcher and test like
val result = mutableListOf<Int>()
val interval = 30.seconds
val flow = getSavedCredentials(interval)
val job = launch {
flow.toList(result)
}
repo.add(1)
advanceTimeBy(interval)
repo.add(2)
advanceTimeBy(interval)
repo.add(3)
advanceTimeBy(interval)
So I would expect result
to be [ 1 ], [ 1, 2 ], [ 1, 2, 3 ]
But instead it is [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ]
What’s wrong with it? 😵David W
09/17/2020, 4:27 PMlaunch {
singletonReceiveChannel.consumeEach { printLine(it) }
}
will produce a memory leak due to consumeEach
never getting canceled, even if `launch`'s CoroutineScope ends?Se7eN
09/17/2020, 6:56 PMusers.postValue(repository.getUsers(20))
, the getUsers function executes but the value isn't posted but when I do list = repository.getUsers(20)
and then do users.postValue(list)
it works totally fine. What's the deal here?Daniele Segato
09/18/2020, 4:24 PMprivate var shared: Deferred<Result>? = null
private suspend fun actuallyGrabResult(): Result { ... }
suspend fun grabResult(): Result {
val deferred = shared.takeUnless( !shared.isActive }
?: async { actuallyGrabResult() }
.also { shared = it }
return deferred.await()
}
The idea is that only 1 actuallyGrabResult()
coroutine at a given time should be running and any request to grabResult()
should wait for it and get the result. Until the result is delivered, next requests will cause another call.
With RxJava you'd do something like:
private val shared: Single<Result> = actuallyGrabResult()
.toObservable()
.replay(1)
.refCount()
.singleOrError()
private fun actuallyGrabResult(): Single<Result> { ... }
fun grabResult(): Single<Result> {
return shared
}
Is there an idiomatic way of doing this with coroutine?
(I've only recently started using coroutines)itnoles
09/18/2020, 5:55 PM(dispatcher as Closeable).close()
inside of
GlobalScope.launch(super.coroutineContext, start = CoroutineStart.ATOMIC)
Intellij IDEA reported it as inappropriate blocking method calls. How I can fix it?Nikita Khlebushkin
09/18/2020, 11:19 PMMikael Alfredsson
09/19/2020, 4:27 PMsuspend fun waitForField(id: String): DocModel? = suspendCancellableCoroutine {
val listener = db.collection("collection").document(id)
.addSnapshotListener { snapshot, e ->
if (snapshot == null) return@addSnapshotListener
val res = snapshot.toObject(DocModel::class.java)
if (res?.calculation != null) {
// here i have to disconnect the listener
it.resume(res)
}
}
it.invokeOnCancellation {
listener.remove()
}
}
kevinherron
09/21/2020, 1:54 PMval serverModel = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
clientAssociation.retrieveModel()
}
where retrieveModel()
throws IOException
. Is this not an appropriate use of <http://Dispatchers.IO|Dispatchers.IO>
?spierce7
09/22/2020, 1:32 AMAnders Sveen
09/22/2020, 7:14 AMbsimmons
09/22/2020, 5:29 PMColton Idle
09/23/2020, 2:24 AMbbaldino
09/23/2020, 5:13 PMfun my update() = runBlocking {
subscribers.forEach { url ->
launch(myIoPool.asCoroutineDispatcher()) {
try {
val resp = client.postJson(...)
<http://logger.info|logger.info>("Got response...")
} catch (e: HttpRequestTimeoutException) {
logger.error("Timed out...")
}
}
}
But I've now realized this is blocking until all the calls to launch
finish. What's the best way to fire-and-forget here? I think I've seen I can do this with GlobalScope.launch
(and getting rid of the runBlocking
), but from what I've read it seems like it's rarely a good idea to use that.Glen
09/24/2020, 1:12 PMGlen
09/24/2020, 1:12 PMlouiscad
09/24/2020, 2:27 PMGlen
09/24/2020, 2:49 PMlouiscad
09/24/2020, 2:55 PMGlen
09/24/2020, 3:00 PMlouiscad
09/24/2020, 3:07 PMGlen
09/24/2020, 3:10 PMlouiscad
09/24/2020, 3:11 PMGlen
09/24/2020, 3:23 PMlouiscad
09/24/2020, 3:43 PMgildor
09/25/2020, 5:42 AMSo I will have to write wrappers for coroutines in the form if callbacks...Serious lot of work...Some C++ Future/Promice class can be exposed instead of callback, so it would be enough to write one single await() function for it, so it will be universal for all async calls to native code But in general it really depends what kind C code you try to integrate
Glen
09/25/2020, 8:47 AMlouiscad
09/25/2020, 8:57 AMgildor
09/25/2020, 9:11 AMGlen
09/25/2020, 9:16 AMgildor
09/25/2020, 9:39 AMThe first is, why can’t I use K/N to avoid JNI altogetherYou can, but then you have to write JNI to talk with K/N from Android It maybe a viable option if most of loigic on k/n side and you have simple interaction with it
How can I add coroutines on top of the JNI API?,JNI api is just Java (or Kotlin JVM) code, no issue to write coroutines adapter on top of it
Glen
09/25/2020, 9:51 AMgildor
09/25/2020, 10:01 AMGlen
09/25/2020, 10:04 AMgildor
09/25/2020, 10:10 AMGlen
09/25/2020, 10:14 AMexternal fun
methods and wrap them with coroutines.gildor
09/25/2020, 10:24 AMGlen
09/25/2020, 10:27 AMgildor
09/25/2020, 10:56 AMGlen
09/25/2020, 11:00 AMgildor
09/25/2020, 2:30 PMGlen
09/26/2020, 12:50 PM