myanmarking
03/17/2020, 1:07 PMdave08
03/17/2020, 2:42 PMasFlow()
on a closed ConflatedBroadcastChannel
will it receive the latest element sent before it was closed, or will the flow be empty?dave08
03/17/2020, 2:44 PMasFlow()
was run on it...Maciek
03/17/2020, 2:57 PMVsevolod Tolstopyatov [JB]
03/17/2020, 3:21 PMFlow.firstOrNull
operators
• java.time
and kotlin.time.Duration
adapters
• More performant and configurable DebugProbes
for usages in production environment
• kotlin-coroutines-jdk9
module for integration with java.util.concurrent.Flow
• Various bug fixes and improvements
Full changelog: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.3.5myanmarking
03/17/2020, 3:32 PMSlackbot
03/18/2020, 1:11 AMJérôme Gully
03/18/2020, 11:22 AMblock
, for the moment I have this (old school hava style) and I guess I can do better with coroutines.
mustStopThread
is a boolean that is false when class is created and true when I must stop the check (when I quit the activity). What I can do better ?
init {
Thread(Runnable {
while (!mustStopThread) {
val lastMaxSoundLevel: Int = mediaRecorder.maxAmplitude
block.invoke(lastMaxSoundLevel)
Thread.sleep(100)
}
}).start()
}
Orhan Tozan
03/18/2020, 6:56 PMclass ViewModel {
val selectedColor: Flow<Color> = ...
fun onNewColorSelect(newColor: Color) {
...
}
}
How can the onNewColorSelect
method update the selectedColor
flow? With Android, you would use MutableLiveData
. How would you solve this problem with Flow?PHondogo
03/18/2020, 7:53 PMclass Test{
@Test
suspend test() {
// test logic with suspend calls
}
}
Orhan Tozan
03/18/2020, 10:16 PMonUserSelect(userId: Int)
I want it to offer an event to a broadcastchannel named userSelectEvents: BroadCastChannel<UserSelectEvent>
. The flow selectedUsers
should depend on this, so I'm probably looking to some kind of building a reducer. This is my idea, is it good?Rômero Ricardo
03/19/2020, 4:03 AMMikołaj Kąkol
03/19/2020, 7:14 AMscope
called bigScope
how can I create a scope smallScope
that will be canceled when bigScope
is cancelled?Orhan Tozan
03/19/2020, 2:35 PMprivate val _selectedUserIds = ConflatedBroadcastChannel<List<Int>>(emptyList())
private val selectedUserIds: Flow<List<Int>> = _selectedUserIds.asFlow()
A onSaveButtonClick()
method inside the class should send a network call with the value of selectedUserIds
Should I get it's value by using the .value
of the ConflatedBroadcastChannel
or should I launch a coroutine and call .collect()
on the Flow
? What's the difference?JP
03/20/2020, 12:16 PMfun main() = runBlocking { // this: CoroutineScope
coroutineScope { // Creates a coroutine scope
println(this)
println(this@runBlocking)
...
and the printed result is:
"coroutine#1":ScopeCoroutine{Active}@64a294a6
"coroutine#1":BlockingCoroutine{Active}@7e0b37bc
Why do they have the same name “coroutine#1”?
If they are different coroutines, shouldn’t they have different labels?
Am I missing something?JP
03/20/2020, 2:29 PMimport kotlinx.coroutines.*
fun main() = runBlocking {
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while (i < 5) { // computation loop, just wastes CPU
// print a message twice a second
if (System.currentTimeMillis() >= nextPrintTime) {
println("job: I'm sleeping ${i++} ...")
nextPrintTime += 500L
}
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
}
The output is:
[DefaultDispatcher-worker-1 @coroutine#2] job: I'm sleeping 0 ...
[DefaultDispatcher-worker-1 @coroutine#2] job: I'm sleeping 1 ...
[DefaultDispatcher-worker-1 @coroutine#2] job: I'm sleeping 2 ...
[main @coroutine#1] main: I'm tired of waiting!
[DefaultDispatcher-worker-1 @coroutine#2] job: I'm sleeping 3 ...
[DefaultDispatcher-worker-1 @coroutine#2] job: I'm sleeping 4 ...
[main @coroutine#1] main: Now I can quit.
From my understanding, the job
did not get cancelled despite the job.cancelAndJoin()
call, because there is no suspending functions (e.g. delay
) which are cancellable (checks for cancellation cooperatively). Therefore the job completes after five iterations, then the job.join()
returns, and then at last “main: Now I can quit.” is printed. Is this correct?
P.S. Does “every” suspending function always check for cancellation?kevin.cianfarini
03/20/2020, 3:20 PMsuspend fun foo() = withContext(Dispatchers.Default) {
for (1..100) {
async { ... }
}
}
Will that be bound to a single thread or will is try to spread the workload?liminal
03/20/2020, 9:20 PMWanted but not invoked observer.onChanged...no interactions with this mock
. Thanks in advance!Raphael Almeida
03/21/2020, 5:16 PMJP
03/22/2020, 6:22 AMimport kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String =
select<String> {
a.onReceiveOrNull { value ->
if (value == null)
"Channel 'a' is closed"
else
"a -> '$value'"
}
b.onReceiveOrNull { value ->
if (value == null)
"Channel 'b' is closed"
else
"b -> '$value'"
}
}
fun main() = runBlocking<Unit> {
val a = produce<String> {
repeat(4) { send("Hello $it") }
}
val b = produce<String> {
repeat(4) { send("World $it") }
}
repeat(8) { // print first eight results
println(selectAorB(a, b))
}
coroutineContext.cancelChildren()
}
The compiler gave warning that onReceiveOrNull
was deprecated:
'onReceiveOrNull: SelectClause1<E?>' is deprecated. Deprecated in favor of onReceiveOrClosed and onReceiveOrNull extension
I guess this is not yet applied in the guide from the documentation. May I ask how I should change the code?
suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String =
select<String> {
a.onReceiveOrClosed {
if (it.isClosed)
"Channel 'a' is closed"
else
"a -> '${it.value}'"
}
b.onReceiveOrClosed {
if (it.isClosed)
"Channel 'b' is closed"
else
"b -> '${it.value}'"
}
}
Would this be the preferred way for now?Chills
03/22/2020, 6:38 AMfcosta
03/23/2020, 2:32 PMheyitsmohit
03/23/2020, 7:23 PMkevin.cianfarini
03/23/2020, 8:15 PMMaciek
03/24/2020, 6:55 AMyield
but with the coroutine that'll be started in the futureluke_c
03/24/2020, 10:41 AMprivate val remoteConfigFetch = lifecycleScope.launch { FirebaseRemoteConfig.init() }
Then further on before the splash screen finally moves on I’m calling join
private fun continueToApp() = runBlocking {
remoteConfigFetch.join()
goToMainActivity()
}
Is this the correct approach? Initially I thought about using async but figured because I don’t care about whether it fails or errors and don’t care about the result it was better to use launch?Gin Wang
03/24/2020, 6:05 PMChannel
still preferred over Flow
for use cases that involve multiple emitters/listeners for a data stream? From what I've read online, Flow is meant to be a safer alternative to Channel, but seems best used for one to one communication?Sam Garfinkel
03/24/2020, 6:27 PMInputStream
or Reader
, what’s the correct way to wrap that such that I can call it as a suspendable function and map some sort of asynchronous communication such as a Channel
into the given InputStream
?Orhan Tozan
03/25/2020, 4:00 AMapi
instead of implementation
?
Also if so, does this also mean for target dependencies (e.g. commonMain: api(coroutines-core-common)
and jvmMain: api(coroutines-core)
)paulex
03/25/2020, 8:16 AMpaulex
03/25/2020, 8:16 AMoctylFractal
03/25/2020, 8:18 AMpaulex
03/25/2020, 8:30 AMtseisel
03/25/2020, 8:31 AMmain
function) on non-UI application due to the lack of an event loop, you could use newSingleThreadContext()
to create a dispatcher to use as your main thread.
Note that you'll have to block the first thread (with runBlocking
and withContext
, or using launch
and awaiting on the resulting job), otherwise the program will terminate.octylFractal
03/25/2020, 8:39 AMwhile
loop thoughpaulex
03/25/2020, 8:47 AMArbitraryMainDispatcher
etc... What am i not reading right?octylFractal
03/25/2020, 8:49 AMArbitraryMainDispatcher
based on the JavaFX
main dispatcher, and gave it queue(Runnable)
as a stand-in for JavaFX's Platform.runLater(Runnable)
. The main part is knowing that MainCoroutineDispatcher
existspaulex
03/25/2020, 8:51 AMtseisel
03/25/2020, 8:53 AMZach Klippenstein (he/him) [MOD]
03/25/2020, 3:14 PMDispatchers.Main
for javafx, is there some reason you can't use that? https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/kotlinx-coroutines-javafx/README.mdoctylFractal
03/25/2020, 8:21 PMZach Klippenstein (he/him) [MOD]
03/25/2020, 9:40 PM