okarm
09/01/2020, 3:28 PMB
as a value of StateFlow<A>
. I would not expect that to be possible. The code not only compiles, it actually runs in the Playground without a run-time exception.Slackbot
09/02/2020, 5:45 AMgenovich
09/02/2020, 9:01 AMList<Deffered<T>>
• List<suspend () -> T>
genovich
09/02/2020, 11:25 AMCoroutineContext
by key and cancel it?Daniele Segato
09/03/2020, 9:23 AMshareIn
/ stateIn
operators... I'm currently planning to work around those by transforming to Rx, using publish().refCount()
or replay().refCount()
and then turning it back to Flow
cause i can't wait anymore for the new operator. I need to stabilize the API so the only other option without those operators is to go with Rx.
When shareIn
will be available I should be able to remove the Rx dependency without having to modify the API interface (hopefully).
Do you know if there's something particular holding back the release of ShareFlow
or if we could expect it to be available soon?Andrea Giuliano
09/03/2020, 2:38 PM.asContextElement()
extension function to make them coroutine-ready. All works well but i find my code having something like this
launch(object1.asContextElement() + object2.asContextElement()) {}
I was thinking to encapsulate those element into a container such that I can do something like this
launch(myContext) {}
where mycontext
contains the elements from object1 and object2.
I was thinking to do this by implementing the CoroutineContext
interface but I then I need to implement all its functions, while what I really want is just the default +
behavior. Is there any nice way to achieve this?Dougie
09/04/2020, 3:32 AMflatMapConcat
one by one.
flatMapMerge
can not do it, and start coroutines will break the flow
are there any good solutions? thanksMaurice Jouvet
09/04/2020, 2:35 PMansman
09/04/2020, 5:14 PMFlow.firstOrNull
restricted to non nullable types? Feels like it'd be useful even with flows with nullable types?bezrukov
09/06/2020, 5:30 PMval flow = flowOf(1, 2)
produce(capacity = 0) { flow.collect { send(it) } }.consumeAsFlow()
.onEach { println("each $it") }
.launchIn(this)
I would expect there will be no difference comparing with:
val flow = flowOf(1, 2)
flow.onEach { println("each $it") }
.launchIn(this)
Link demonstrates unnecessary redispatch: https://pl.kotl.in/LbbXNwWzURobert von Massow
09/07/2020, 9:34 AMrunBlocking {
val coroutineScope = CoroutineScope(EmptyCoroutineContext)
val flowScope = CoroutineScope(EmptyCoroutineContext)
flowScope.launch {
val flow = flowOf(1)
while (isActive)
flow.collect { i ->
println("received $i")
coroutineScope.launch {
// Offload some hard work
println("processing $i")
Thread.sleep(1000)
println("done processing $i")
}
}
}
// just added so it doesn't terminate right away
delay(10000)
}
Glen
09/07/2020, 10:27 AMrrva
09/07/2020, 10:47 AMprivate val concurrentRequests = Semaphore(50)
private suspend fun <T> requestWithLimit(block: suspend () -> T): T {
try {
withTimeout(10) {
concurrentRequests.acquire()
}
} catch (e: Exception) {
throw RuntimeException("Overloaded", e)
}
try {
return block()
} finally {
concurrentRequests.release()
}
}
I usually call io.ktor.client.HttpClient.request inside block(). If I want to write it as an extension function on io.ktor.client.HttpClient (just to make the call site a bit cleaner), how would I define it? It seems that the extension function cannot access the variable concurrentRequests
Ale
09/07/2020, 11:29 AM@ExperimentalCoroutinesApi
private fun retrieveFromFirestore(porteroId: String): Flow<Portero> = callbackFlow {
val porteroCallback = EventListener<QuerySnapshot> { querySnapshot, e ->
val element = //extract data from snapshot etc.
launch { //save to local sqllite }
offer(element)
}
}
This works well.
However, when I tried to refactor the code a bit, and extract the snapshot manipulation to another method:
@ExperimentalCoroutinesApi
private fun retrieveFromFirestore(porteroId: String): Flow<Portero> = callbackFlow {
val porteroCallback = EventListener<QuerySnapshot> { querySnapshot, e ->
val element = buildData(querySnapshot, e)
offer(element)
}
}
private buildData(querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException?): Element {
//extract data from snapshot etc.
launch { //save to local sqllite } // <--- this doesn't compiles
return element
}
The launch function is no longer available, with the error Unresolved reference.
I can fix it using CoroutineScope(Dispatchers.Default).launch { ... }
but why is this happening? How come launch is available only in the calling function but not in the called one?Hakob Vardanyan
09/07/2020, 2:39 PMflowOn, launchIn, etc
functions will be released?George Theocharis
09/07/2020, 3:31 PMMiSikora
09/07/2020, 7:37 PMkotlin.Result.Failure
throws instead of delivering value when continuation is resumed on a different thread?
suspend fun main() {
val failure = fail()
println(failure)
}
suspend fun fail() = suspendCoroutine<Result<String>> {
Executors.newSingleThreadExecutor().execute {
it.resume(Result.failure(RuntimeException()))
}
}
Exception in thread "main" java.lang.RuntimeException
at MainKt$fail$2$1.run(main.kt:12)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
gotoOla
09/07/2020, 10:16 PMFernando Branbila Cunha Junior
09/08/2020, 12:45 AMfun main() {
GlobalScope.launch {
val deferredUser = getUserByIdAsync(1)
val data = deferredUser.await()
print(data)
}
}
private suspend fun getUserByIdAsync(userId: Int) =
GlobalScope.async {
println("Retrieving user from network")
delay(3000)
println("Still on coroutine")
User(userId, "John", "Silva")
}
Erik
09/08/2020, 1:30 PMdebounce
input. I'd like to know if some value is currently being debounced (and I'd like to know the value), so that I don't have to wait for it to be collected downstream. How would I do that?Joe
09/08/2020, 10:47 PMsuspend
method that gets dynamically overridden by Guice AOP. It appears that if I have a method handle and use method.kotlinFunction.callSuspend(obj, *args)
to call that method, it fails because the wrapped method's isSuspend
property is false (probably because it's dynamically generated by something that is not the kotlin compiler and doesn't know about suspend functions). It looks like I can use suspendCoroutineUninterceptedOrReturn { method.kotlinFunction.call(obj, *args, it)}
to bypass the isSuspend
check, but is that safe or is there another approach I should take?Mijael Viricochea
09/09/2020, 2:21 PM@ExperimentalCoroutinesApi
fun deleteSelected() {
val value = dataPoints.value
value.removeAll { it.selected }
dataPoints.value = ArrayList(value)
}
dan.the.man
09/09/2020, 2:31 PMFragment {
lifecycleScope.launch { vm.attachListeners }
}
VM {
val locationListener:ReceiveChannel<Location>
val navListener:ReceiveChannel<NavEvent>
suspend fun attachListeners(){
locationListener.consumeEach{//Do stuff}
navListener.consumeEach{//do stuff}
}
With the code above, if this was RxJava, it would behave correctly. With Coroutines the first .consumeEach
is blocking it appears, never executing the second one.
I can change it to
suspend fun attachListeners(){
coroutineScope{
launch{
locationListener.consumeEach{//Do stuff}
}
launch {
navListener.consumeEach{//do stuff}
}
}
}
And that will work. The goal is to make it automatically cancel with the lifecycleScope, I'm wondering is there a better way of doing this? And why does .consumeEach block?Rechee Jozil
09/10/2020, 5:03 AMflosch
09/10/2020, 5:36 AMFlow<T>.throttleFirst()
here. @elizarov wrote that to test such an operator we would have to use the DelayController
if available instead of System.currentTimeMills()
, which of course makes total sense. My question: How could I publish this library without having kotlinx-coroutines-test
in the release dependencies? Meaning how would I prevent the user of my library having kotlinx-coroutines-test
in their release binaries when they use my dependency?KayCee
09/10/2020, 8:45 AMbsimmons
09/10/2020, 12:57 PMdelay {}
and runBlocking {}
working on iOS/native. Currently, I am trying out <http://1.3.5-native.mt|1.3.5-native.mt>
and am getting some bizarre/arbitrary freezing errors that would otherwise work on the JVM. What is the recommended way of launching coroutines on iOS? Is there a guide or template anywhere? Is it worth upgrading to 1.4 and using the latest version?Fernando Branbila Cunha Junior
09/10/2020, 1:05 PMval bar = async { foo() }
Or there's a way to do like:
async { val bar = foo() }
and then use bar value?Erik
09/10/2020, 5:41 PMsuspend fun main()
for the first time. Where is it documented? How does it work?zak.taccardi
09/10/2020, 8:05 PMCoroutineScope
, what is the idiomatic way to create a child scope?zak.taccardi
09/10/2020, 8:05 PMCoroutineScope
, what is the idiomatic way to create a child scope?wasyl
09/10/2020, 8:06 PMcoroutineScope { }
zak.taccardi
09/10/2020, 8:08 PM(CoroutineScope) -> CoroutineScope
where the output is a child scopeval globalSdkScope: CoroutineScope
val childScope = globalSdkScope + Job(globalSdkScope.coroutineContext[Job]!!)
Casey Brooks
09/10/2020, 8:18 PMzak.taccardi
09/10/2020, 8:18 PMCasey Brooks
09/10/2020, 8:21 PMzak.taccardi
09/10/2020, 8:21 PMZach Klippenstein (he/him) [MOD]
09/10/2020, 8:45 PMzak.taccardi
09/10/2020, 8:46 PMpublic fun CoroutineScope.newChildScope(
context: CoroutineContext = EmptyCoroutineContext
): CoroutineScope {
val parentScope = this
return parentScope + parentScope.job.newChildJob() + context
}
public fun Job.newChildJob(): Job {
val parentJob = this
return Job(parent = parentJob)
}
gildor
09/10/2020, 11:53 PMzak.taccardi
10/06/2020, 10:18 PMGlobalScope
should never be used, thoughgildor
10/06/2020, 11:35 PM