https://kotlinlang.org logo
#compose
Title
# compose
c

Chris Fillmore

06/07/2022, 3:28 PM
My app uses camera2 and I map the CameraCharacteristics for the camera currently in use, in a Flow that I collect in a screen in my app. I am routinely getting this exception (in 🧵 )
Copy code
E  java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
E      at androidx.compose.runtime.snapshots.SnapshotKt.readError(Snapshot.kt:1791)
E      at androidx.compose.runtime.snapshots.SnapshotKt.readable(Snapshot.kt:1786)
E      at androidx.compose.runtime.snapshots.SnapshotKt.readable(Snapshot.kt:1777)
E      at androidx.compose.runtime.SnapshotMutableStateImpl.getValue(SnapshotState.kt:130)
E      at com.example.android.camera.LiveLiterals$CameraCharacteristicsExtKt.Boolean$arg-1$call-EQEQ$fun-$get-hasFlashUnit$$get$val-hasFlashUnit(Unk
  nown Source:27)
E      at com.example.android.camera.CameraCharacteristicsExtKt.getHasFlashUnit(CameraCharacteristicsExt.kt:68)
(stack trace edited to remove my package name)
This occurs when calling this extension property
Copy code
val CameraCharacteristics.hasFlashUnit: Boolean get() {
  return get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true
}
My composable screen is roughly
Copy code
@Composable
fun CameraScreen(
  ...
  currentCameraCharacteristics: CameraCharacteristics,
) {
  if (currentCameraCharacteristics.hasFlashUnit) {
    // show an icon to enable the torch
  }
}
Called like so
Copy code
val currentCameraCharacteristics by cameraOwner.currentCameraCharacteristics.collectAsState()
CameraScreen(
  ...,
  currentCameraCharacteristics = currentCameraCharacteristics,
)
There are about a dozen other flows collected and shown in this screen; but I occasionally get this exception for this exact situation. And I only get it for this
I would guess it happens maybe 1 in 50 times I run the app. Possibly less frequently
I’m disturbed somewhat by the presence of LiveLiterals in this stack trace
Copy code
LiveLiterals$CameraCharacteristicsExtKt.Boolean$arg-1$call-EQEQ$fun-$get-hasFlashUnit$$get$val-hasFlashUnit(Unk
  nown Source:27)
Is this only happening when I’m editing code while running the app?
Thanks for any help!
c

Colton Idle

06/07/2022, 3:57 PM
Maybe disable live literals and see if that's the culprit?
c

Chris Fillmore

06/07/2022, 3:57 PM
Yeah that seems a good place to start. It occurs so infrequently I’m not sure I’ll notice though
c

Colton Idle

06/07/2022, 3:57 PM
I guess LiveLiterals is going away in favor of Live Edit anyway. So you can try that?
c

Chris Fillmore

06/07/2022, 3:59 PM
Ooh thanks for the tip. I’ll check it out
c

Colton Idle

06/07/2022, 4:01 PM
im also in electric eel though, so you may not see those options (they unfortunately dont show when you search for them using the intellij action search)
but you should still be able to disable live literals in stable or beta.
👍 1
32 Views