Why onEach instead of collect {} #846
Replies: 3 comments
-
I'm curious about this too. I did find a session from the Android Dev Summit 2021 where they mention the use of Also, I stumbled upon this discussion post while wondering why we don't use lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.collectLatest { uiState = it }
}
} Hoping for anyone with more experience in Kotlin Flows to comment on whether this is a valid (and better) code. |
Beta Was this translation helpful? Give feedback.
-
I think it's written that way when he considers a lot of maintenance of functions. |
Beta Was this translation helpful? Give feedback.
-
@ParkJong-Hun |
Beta Was this translation helpful? Give feedback.
-
I'm trying to learn best practices and I'm not sure why you are using in the MainActivity .onEach instead of just simply using collect {} directly, just personal preference?
Beta Was this translation helpful? Give feedback.
All reactions