hi , It ok to do this in a single launch ? or do w...
# android
a
hi , It ok to do this in a single launch ? or do we have to use separate launch for repeat on life cylce ? lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginAuthorizationViewModel.onLoginSuccessful.collect { startActivity(HomeActivity.getDefaultIntent(this@LoginActivity)) finish() } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onPasswordInvalid.collect { enableViews(true) et_password.error = it } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginAuthorizationViewModel.onLoginFailure.collect { enableViews(true) showMessage(it.toString()) } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onUserNameInvalid.collect { enableViews(true) tv_user_name.error = it } } lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { loginInputValidationViewModel.onValidationSuccessful.collect { loginAuthorizationViewModel.requestLogin( userName = it.first, password = it.second ) } }
🧵 3
j
I think under the hood
repeatOnLifecycle
launch its own coroutines
a
@Javier my trial shows it doesn't work. I feel there are better ways to do this, however i just don't know how.
we have to use different launch
a
Separate launches within repeat on lifecycle
Call launch on all inner suspends
g
Inside
repeatOnLifecycle
, for every flow you can do
flow.onEach { do something }.launchIn(this)
(where
this
is the coroutine scope). It is equivalent to
launch { flow.collect { do something } }