One Off the Slack: My Kingdom for a Frame!

If you attempt to modify a MutableState, you might get a “Not in a frame” error, as Lionell Pack discovered:

java.lang.IllegalStateException: Not in a frame
        at androidx.compose.frames.FramesKt.currentFrame(Frames.kt:180)
        at androidx.compose.frames.FramesKt.writable(Frames.kt:466)
        at androidx.compose.frames.FramesKt._writable(Frames.kt:462)

According to Google’s Leland Richardson, the issue is attempting to modify the state from a background thread:

This is something that is possible to do currently, but you have to “open a frame” which can be thought of as similar to a database transaction. the APIs to do this we haven’t talked about much and aren’t very discoverable, because instead we are making this exception go away entirely so that what you are doing currently will be valid in the future

Depending upon your situation, you might be able to arrange to update the state on the main thread (e.g., by observing LiveData rather than directly mutating a state). Alternatively, you can wrap your modification in a frame manually:

FrameManager.framed {
  // TODO modify the state here
}

Hopefully, all this will go away in the not-too-distant future, as Leland hinted at.


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!