Closed pchmielowski closed 5 years ago
You're passing debugMode = true
to your ViewModel. In Debug mode, MvRx runs your state reducers twice to make sure that your state is immutable. Since you modify state as well as perform a side effect (invocations++
) in the state reducer, you get the side effect performed twice.
Please refer to this page for more information on this: Debug Checks
Your onLoginClick
method should look something like this:
fun onLoginClick() = withState { state ->
if (state.login.isEmpty()) {
setState { copy(status = Status.Error(LoginError.EmptyLogin)) }
} else {
viewModel.performLogin()
setState {
copy(status = Status.Pending)
}
}
Ok, now I understand! Thank you for the explanation and correct code example ;)
Here is a simple test case, which shows that reducer passed as an argument to
setState
method is invoked twice, even if the caller (methodfoo()
) is invoked just once.The result:
Is it a desired behavior or a bug? This behavior causes some problems for me, because in my real application I have a
ViewModel
with the following method:The problem is that
performLogin
method is invoked twice, which leads to two server requests.