https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

pers

02/22/2024, 3:40 PM
I following MVI pattern howevery I feel it is a show to put event in a stream then collect it , I want to make handleEvent running asyc without adding with withContext in CardNumberChanged,NameOnCardChanged
Copy code
private fun collectEvents() {
        viewModelScope.launch {
            withContext(Dispatchers.Default) {
                events
                    .collect {
                        handleEvent(it)
                    }
            }
        }
    }
Here is a
Copy code
override fun handleEvent(event: CheckoutViewModelContract.Event) {
    when (event) {
        CheckoutViewModelContract.Event.Initial -> onInitial()
        is CheckoutViewModelContract.Event.CardNumberChanged -> onCardNumberChanged(event.text)
        is CheckoutViewModelContract.Event.NameOnCardChanged -> onNameOnCardChanged(event.text)
        is CheckoutViewModelContract.Event.ExpirationDateChanged -> onCardExpirationDateChanged(
            event.text
        )
c

Casey Brooks

02/22/2024, 3:54 PM
You can provide the dispatcher directly to the
.launch
call
Copy code
viewModelScope.launch(Dispatchers.Default) { }
🙌 1