I'm struggling with the following error when attem...
# compose
r
I'm struggling with the following error when attempting to update my state from a WebSocket callback:
java.lang.IllegalStateException: Not in a frame
at androidx.compose.frames.FramesKt.currentFrame(Frames.kt:180)
W/System.err: at androidx.compose.frames.FramesKt.writable(Frames.kt:466)
at androidx.compose.frames.FramesKt._writable(Frames.kt:462)
I'm completely new to Android dev - most of my mobile dev before has been using xplat frameworks like Xamarin and React Native. I've managed to update to dev12. I suspect the problem is related to not being on the UI thread, but being so new to Android dev in general, and there not being many examples out there because Compose is so new, my Google-fu is failing me. Coroutines keep coming up but I can't quite work out the api. Can someone point me in the right direction?
l
you are correct, this is happening as a result of you mutating a state variable off of the main 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
in the near term, what you can do is one of two things: 1. switch back to the main thread before doing your mutation 2. open a “frame”, by calling
FrameManager.framed { myMutation() }
and don’t feel bad, this is a really unhelpful error message and not a very intuitive situation right now. it should be improving soon though!
r
Brilliant! Thanks so much, I've been beating my head against this for a while. That's fixed it and I can forge ahead with my pet project 😄
For the record, I've found Compose to be a really pleasant experience so far. Between Compose and SwiftUI I can certainly see more native projects in my future! (I work at a consultancy which build a lot of mobile apps, among other things.)
👍 3
🎉 1
v
I noticed this error only in a very specific usecase. When I run it on the app it works fine, but I get this error when I’m running the UI tests. I wonder what it could be related to as I wasn’t switching the threads at all but maybe the test runners are doing something. I wrapped it inside
runOnUiThread
to avoid it like you recommended above.