Alexander Black
09/02/2021, 12:29 AMval scope = MainScope() + Job()
val scope2 = MainScope() + Job()
val someEmitter = MutableStateFlow("")
fun test3()= runBlocking {
scope.launch {
someEmitter.emit("1")
someEmitter.emit("2")
someEmitter.emit("3")
}
scope2.launch {
someEmitter.collect {
println(it)
}
}
}
why does scope2 collect operation get blocked by scope? do they have to be on different threads? Am I missing something? I’ve gotten this type of thing to work before, but only by using another thread as the collector.Lefko
09/02/2021, 6:45 AMval scope = MainScope() + Job()
val scope2 = MainScope() + Job()
val someEmitter = MutableStateFlow("")
fun test3()= runBlocking {
scope2.launch {
someEmitter.collect {
println(it)
}
}
scope.launch {
someEmitter.emit("1")
someEmitter.emit("2")
someEmitter.emit("3")
}
}
wasyl
09/02/2021, 7:07 AMMainScope
does print 3
for me :blob-thinking-upside-down:
@Test
fun name(): Unit = runBlocking {
val scope = CoroutineScope(EmptyCoroutineContext) + Job()
val scope2 = CoroutineScope(EmptyCoroutineContext) + Job()
val someEmitter = MutableStateFlow("")
scope.launch {
someEmitter.emit("1")
someEmitter.emit("2")
someEmitter.emit("3")
}
scope2.launch {
someEmitter.collect {
println(it)
}
}
}
emit
is suspendable, it doesn’t actually suspend as far as I understand. If there’s no suspension, the body of the second launch
isn’t even called until after you emit the last value. This code:
@Test
fun name(): Unit = runBlocking {
val scope = CoroutineScope(EmptyCoroutineContext) + Job()
val scope2 = CoroutineScope(EmptyCoroutineContext) + Job()
val someEmitter = MutableStateFlow("")
println("Launching first scope")
scope.launch {
println("Launching code in first scope")
println("Emitting 1")
someEmitter.emit("1")
println("Emitting 2")
someEmitter.emit("2")
println("Emitting 3")
someEmitter.emit("3")
}
println("Launching second scope")
scope2.launch {
println("Launching code in second scope")
someEmitter.collect {
println(it)
}
}
}
will print
Launching first scope
Launching second scope
Launching code in first scope
Emitting 1
Emitting 2
Emitting 3
Launching code in second scope
3
and this is expected. First both launches
are called, then the runBlocking
suspends so the first launch
executes its body. Nothing inside suspends, so the first launch
completes before execution moves to the second launch
.MutableStateFlow
anyway. It represents a state, and collectors aren’t guaranteed to get the intermediate emissions, but the final emitted state should always be correctAlexander Black
09/02/2021, 3:55 PMfun test3()= runBlocking {
val scope = CoroutineScope(EmptyCoroutineContext) + Job()
val scope2 = CoroutineScope(EmptyCoroutineContext) + Job()
val someEmitter = MutableSharedFlow<String>(1,0)
scope2.launch {
someEmitter.collect {
println(it)
}
}
scope.launch {
someEmitter.emit("1")
someEmitter.emit("2")
someEmitter.emit("3")
}
}
Raheel Naz [Amex]
10/01/2021, 6:49 AMwasyl
10/01/2021, 7:25 AMJob()
directly instead of plussing, I think