Skip to content

Instantly share code, notes, and snippets.

@houssemzaier
Last active September 30, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save houssemzaier/603ce09b198dfded7fcd08b42ea5f764 to your computer and use it in GitHub Desktop.
Save houssemzaier/603ce09b198dfded7fcd08b42ea5f764 to your computer and use it in GitHub Desktop.
onEach and flowOn
package fr.x
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import org.junit.Test
/**
* Unit tests of [MaasPaymentMethodsViewModel].
*/
class MaasPaymentMethodsViewModelTest {
@Test
fun main() {
println(
"""
*Start: running in : ${Thread.currentThread()}
""".trimIndent(),
)
runBlocking {
flow {
for (i in 1..5) {
emit(i)
delay(200)
println(
"""
*emitting: running in : ${Thread.currentThread()}
""".trimIndent(),
)
}
}
.onEach {
delay(100)
println(
"""
*delaying: running in : ${Thread.currentThread()}
""".trimIndent(),
)
}
.flowOn(Dispatchers.IO)
.collect { println(it) }
}
}
}
@houssemzaier
Copy link
Author

*Start: running in : Thread[Test worker,5,main]
*delaying: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
1
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
2
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
3
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
4
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
5
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]

@houssemzaier
Copy link
Author

changing the code to this:

.flowOn(Dispatchers.IO)
 .onEach {
  delay(100)
println(
 """ 
 *delaying: running in : ${Thread.currentThread()}
""".trimIndent(),
}
.collect { println(it) }

will give this output:
*Start: running in : Thread[Test worker,5,main]
*delaying: running in : Thread[Test worker @coroutine#1,5,main]
1
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[Test worker @coroutine#1,5,main]
2
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[Test worker @coroutine#1,5,main]
3
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[Test worker @coroutine#1,5,main]
4
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]
*delaying: running in : Thread[Test worker @coroutine#1,5,main]
5
*emitting: running in : Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment