Reputation: 1616
Anyone please advise what I can do this. I do have a state variable that is updated in the composition and elsewhere. Do I need a lock to ensure that it is not changed at the same time?
AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Unsupported concurrent change during composition. A state object was modified by composition as well as being modified outside composition.
at androidx.compose.runtime.Recomposer.applyAndCheck(Recomposer.kt:825)
at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1092)
at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:105)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:456)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:425)
at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1090)
at android.view.Choreographer.doCallbacks(Choreographer.java:893)
at android.view.Choreographer.doFrame(Choreographer.java:809)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
I/Process: Sending signal. PID: 15089 SIG: 9
Upvotes: 7
Views: 2598
Reputation: 211
Assuming the mutable state variable is being modified both inside and outside of composition, you can refactor the external modifications to use events or callbacks/lambda functions.
If the state is being modified as a result of a side effect, consider using LaunchedEffect to manage these side effects within the Compose lifecycle.
If the mutable state value is modified in viewmodel, try to update the value inside viewModelScope.
Change the mutable state in viewModel to MutableStateFlow and use collectAsState() in composable to collect it as a state.
Upvotes: 0
Reputation: 76
This may help you, read about side effects. I think you should use one of this rememberCoroutineScope , LaunchedEffect..
Read more:
https://developer.android.com/jetpack/compose/side-effects
Upvotes: 1