Marc Knaup
10/13/2020, 1:32 AM(Mutable)StateFlow
may be useful 🙂
val state by someStateFlow()
…
println(state)
var foo by someMutableStateFlow()
…
state = newFoo()
Orhan Tozan
10/13/2020, 1:22 PMStateFlow<T>
mapping a planned feature? Right now I stick with Flow instead of StateFlow<T> because it stops me from doing something like this:
// Example 1
val name: StateFlow<String> = ...
val greeting: StateFlow<String> = name.map { name ->
"Hi, $name!"
}
// Example 2
val a: StateFlow<Int> = ...
val b: StateFlow<Int> = ...
val sum: StateFlow<Int> = combine(a, b) {a, b -> a + b }
Vsevolod Tolstopyatov [JB]
10/13/2020, 1:42 PMSharedFlow
for managing hot sources of data
• shareIn
and stateIn
operators to transform a cold Flow
into a hot one
• Channel cancellation atomicity is reworked, and API for closeable resource transfer is introduced
And more, full changelog: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.4.0-M1zak.taccardi
10/13/2020, 4:21 PMMarc Knaup
10/13/2020, 6:34 PMNo coroutine information found
Target VM is not paused by breakpoint request. Evaluation of methods is not possible in this mode
dalewking
10/13/2020, 10:21 PMMarc Knaup
10/14/2020, 1:03 PMval coroutineContext = parentScope.coroutineContext + CoroutineName("foo")
Is there a difference between GlobalScope.launch(coroutineContext) {…}
and CoroutineScope(coroutineContext).launch {…}
?Marc Knaup
10/14/2020, 1:14 PMMutableStateFlow
is great for passing around a shared cache, or is it? 🤔CLOVIS
10/14/2020, 5:40 PMval input: Flow<MyObject> = ...
val output = input.map { it.toOtherObject() }
I would like the calls to toOtherObject
to be ran in parallel, and I would like an exception in one call not to stop the others.
I tried:
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
val output = input.map {
scope.async {
it.toOtherObject()
}
}.map { it.await() }
But this is still sequential, and not parallel. What am I doing wrong?Oleg Siboglov
10/14/2020, 7:09 PMjoining
occur before second started
? I’m running this in IntelliJ if that makes any difference.
fun main() = runBlocking {
val job = GlobalScope.launch {
val deferredOne = async(<http://Dispatchers.IO|Dispatchers.IO>) {
println("first started")
delay(1_000)
println("first finished")
}
val deferredTwo = async(<http://Dispatchers.IO|Dispatchers.IO>) {
println("second started")
delay(2_500)
println("second finished")
}
println("joining")
deferredOne.join()
deferredTwo.join()
println("complete")
}
job.join()
}
This is the output.
first started
joining
second started
first finished
second finished
complete
Marc Knaup
10/15/2020, 12:47 AMif (coroutinesAvailable) GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) { … } else …
Animesh Sahu
10/15/2020, 3:54 AMFlow<T>.collectWhile(predicate)
is internal in kotlinx.coroutines.flow ?
It could be useful to suspend a coroutine till value matches some predicate. Is Flow<T>.first(predicate): T
considered to be used (as it utilizes the collectWhile) or something else to be used?Marc Knaup
10/15/2020, 8:07 PMRicardo C.
10/16/2020, 2:59 PMwhy
10/16/2020, 9:41 PMursus
10/17/2020, 5:07 AMursus
10/17/2020, 5:12 AMCLOVIS
10/17/2020, 12:21 PMFlow.flatMapMerge
?
I have this:
val f: Flow<CustomObject> = ...
f.flatMapMerge {
it.customFlow()
}
This works fine as long as there is no exception, however if there is one, the entire flow is cancelled. I tried to write
f.flatMapMerge {
try {
it.customFlow()
} catch(e: Throwable) {
println("Caught")
emptyFlow()
}
}
but that still cancels the flow, instead of just skipping items for which the conversion fails.ziv kesten
10/17/2020, 1:54 PMiex
10/17/2020, 4:16 PMObservable.just
with channels?ursus
10/18/2020, 2:17 AMclass MainActivity : AppCompatActivity() {
private val scope = MainScope()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("Default", "a")
scope.launch {
Log.d("Default", "b")
flowOf(1)
.collect { Log.d("Default", "coll=$it t=${Thread.currentThread().name}") }
}
Log.d("Default", "c")
}
}
prints:
2020-10-18 04:14:26.808 21786-21786/sk.foo.flowplayground D/Default: a
2020-10-18 04:14:26.808 21786-21786/sk.foo.flowplayground D/Default: c
2020-10-18 04:14:26.848 21786-21786/sk.foo.flowplayground D/Default: b
2020-10-18 04:14:26.851 21786-21786/sk.foo.flowplayground D/Default: coll=1 t=main
I want:
2020-10-18 04:14:26.808 21786-21786/sk.foo.flowplayground D/Default: a
2020-10-18 04:14:26.808 21786-21786/sk.foo.flowplayground D/Default: b
2020-10-18 04:14:26.848 21786-21786/sk.foo.flowplayground D/Default: coll=1 t=main
2020-10-18 04:14:26.851 21786-21786/sk.foo.flowplayground D/Default: c
dorf
10/18/2020, 7:57 AMcaelum19
10/18/2020, 2:46 PMClass 'androidx.compose.ui.layout.LayoutCoordinates' is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler
etcursus
10/18/2020, 4:37 PMviewModel.state
flow in android Activity, between onStart and onStop
I do that, and notice that after having activity.onStop called, next activity.onStart doesn't start listening to the flow
So I conclude scopes are not reusable
But this is android reality and I need that. I need equivalent of rx compositeDisposable.clear()
Is the only solution to have abstraction over the scope which will create new scope instances?Joaquim Ley
10/18/2020, 6:15 PM1.3.9-native-mt-2
it works on iOS but not for Android
• const val kotlinCoroutines = “1.3.9-native-mt-2”
With 1.4.0-M1
works for android but not iOS
• const val kotlinCoroutines = “1.4.0-M1"nitrog42
10/19/2020, 9:36 AMLuis Munoz
10/19/2020, 8:01 PMscope.launch {
while (isActive) {
launch {
val record = aKafkaConsumer.poll(Duration.ofMillis(200))
log.debug().log("[received records] - count={}", record.count())
record.forEach {
callback(KafkaMessage(it.key(), it.value()))
}
}
}
}
scope.launch {
while (isActive) {
val record = aKafkaConsumer.poll(Duration.ofMillis(200))
log.debug().log("[received records] - count={}", record.count())
record.map {
launch { callback(KafkaMessage(it.key(), it.value())) }
}.joinAll()
}
}
Rechee Jozil
10/19/2020, 9:52 PMntherning
10/20/2020, 6:48 AMMutex.withLock
and unexpected freezing in my iOS app when upgrading my project to kotlin 1.4.10 and coroutines 1.3.9-native-mt-2. I think I have been able to make a simple reproduction case:
runBlocking(newSingleThreadContext("thread")) {
val mutex = Mutex().apply { ensureNeverFrozen() }
launch {
runCatching { mutex.withLock { delay(1000) } }.exceptionOrNull()?.printStackTrace()
}
launch {
runCatching { mutex.withLock { delay(1000) } }.exceptionOrNull()?.printStackTrace()
}
}
This launches two coroutines in the same worker. Both tries to take the lock and then delays for 1 sec while holding it. As everything happens within the same worker thread I would not expect any mutability issues. But... This is what happens:
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.sync.MutexImpl.LockedQueue@788375b8
at kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (kotlin/kotlin/Throwable.kt:23:37)
at kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (kotlin/kotlin/Exceptions.kt:23:44)
at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (kotlin/kotlin/Exceptions.kt:34:44)
at kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} + 91 (kotlin/kotlin/native/concurrent/Freezing.kt:22:60)
at ThrowInvalidMutabilityException + 690 (kotlin/kotlin/native/concurrent/Internal.kt:92:11)
at MutationCheck + 108
at kfun:kotlinx.coroutines.sync.MutexImpl.LockedQueue.<set-owner>#internal + 102 (sync/Mutex.kt:350:19)
at kfun:kotlinx.coroutines.sync.MutexImpl#unlock(kotlin.Any?){} + 3740 (sync/Mutex.kt:327:29)
at kfun:MutexInvalidMutabilityTest.$doTest$lambda-2$lambda-0COROUTINE$0.invokeSuspend#internal + 2206 (MutexInvalidMutabilityTest.kt:16:61)
...
You can find the full code and full stack trace it in this gist:
https://gist.github.com/ntherning/a1bf85d44a5989d66677762c15587552
Is this expected behavior? Perhaps I am doing something stupid? Or a bug in MutexImpl
?joney
10/20/2020, 7:47 AMSharedFlow
is planned to be included in native-mt
?joney
10/20/2020, 7:47 AMSharedFlow
is planned to be included in native-mt
?gildor
10/20/2020, 7:59 AMlouiscad
10/20/2020, 8:45 AMdalewking
10/26/2020, 1:00 PMlouiscad
10/26/2020, 1:01 PM