Chachako
09/19/2021, 12:17 PMlaunch { send(..) }
in produce {}
and ensure I can receive them in produce.consumeEach
?Chachako
09/19/2021, 12:22 PMproduce {
list.forEach {
coroutineScope {
it.nestedList.forEach { sub ->
launch { send(sub) }
}
}
}
}.consumeEach { println(it) }
It seems that the channel has ended before all the data is sendJoffrey
09/19/2021, 1:12 PMsend
instead of coroutinScope + launch + send
?Joffrey
09/19/2021, 1:16 PMJoffrey
09/19/2021, 1:17 PMchannelFlow
instead of produce
to get a Flow
instead, which may be more convenient to work with (but that may depend on your use case)Chachako
09/19/2021, 1:34 PMJoffrey
09/19/2021, 1:37 PMcoroutineScope
+ mutiple nested launch
(or maybe async
if you want results) seems appropriate for that, but the example code above seemed to suggest you were sending elements from a list, so it would have been unnecessary. If you want to make concurrent network calls, this is correct. You just have to be careful about whether you're blocking any threads etc. If these calls are blocking you might want to dispatch them using <http://Dispatchers.IO|Dispatchers.IO>
Chachako
09/19/2021, 2:48 PMsend
for a long time to decide or something else? I asked this question because I found something missing in the result after running (it is determined that it was call send
). I will try to restore a code to recover this errorJoffrey
09/19/2021, 2:50 PMclose()
on the producer side, but produce
does it automatically at the end of the block when you reach it. Since you have put coroutineScope
around your launch
calls, they should all have finished by the time you reach the end of the block, so you should be fine. Maybe you had this error without coroutineScope
?Joffrey
09/19/2021, 2:54 PMproduce
would also automatically be closed if an exception is thrown somewhere in the produce
block.Chachako
09/19/2021, 3:27 PM