Error:
Recomposer.applyAndCheck
java.lang.IllegalStateException - Unsupported concurrent change during composition. A state object was modified by composition as well as being modified outside composition.
From time to time I have error with unsupported concurrent change.
I try to understand the source of the problem. When I was learning composable I was using coroutineScope with Dispatchers.IO
to update state and it cause the same problem. I have read https://developer.android.com/develop/ui/compose/side-effects and I do not see any issues in my code.
What kind of problem is this?
Maybe during changes on state I should use viewModelScope~?
is HomeEvent.OnFreeCouponDeclined -> {
homeState.update {
copy(isDeclined = false)
}
}
should it be?
is HomeEvent.OnFreeCouponDeclined -> {
viewModelScope.launch {
homeState.update {
copy(isDeclined = false)
}
}
}
Maybe here, could it be wrong?
private fun observeInventory() {
viewModelScope.launch {
(Dispatchers.Default) {
getInventoryInteractor.invoke().collectLatest {
(Dispatchers.Main) {
homeState.value = homeState.value.copy(categories = it.map {
it.copy(subCategories = emptyList())
})
}
}
}
}
}
It looks I sometimes use viewModelScope
to change state and sometimes not.
Please I kindly ask you for advice, what I could look for?
Maybe this code?
LaunchedEffect("${state.currentPage}_${sliderHoldTimestamp}_${sliderItems.size}") {
delay(AUTO_SLIDER_DELAY)
coroutineScope.launch {
if (sliderItems.isNotEmpty() && state.pageCount != 0) {
var newPosition = state.currentPage + 1
if (newPosition > sliderItems.size - 1) newPosition = 0
state.animateScrollToPage(newPosition.mod(state.pageCount))
}
}
}
This is auto slider, I had to workaround it like this because of the update from accompanist
to newer approach.
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
const val foundation = "androidx.compose.foundation:foundation:1.6.1"