How come I get this error ```java.lang.IllegalStat...
# compose
m
How come I get this error
Copy code
java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied                                                                                                    	at androidx.compose.runtime.snapshots.SnapshotKt.readError(Snapshot.kt:1865)                                                                                                  	at androidx.compose.runtime.snapshots.SnapshotKt.readable(Snapshot.kt:1849)                                                                                                    	at androidx.compose.runtime.SnapshotMutableStateImpl.getValue(SnapshotState.kt:130)                                                                                                    	at asia.zeals.mobile.android.viewmodels.AppStateViewModel.getScreenState(AppStateViewModel.kt:51)                                                                                                    	at asia.zeals.mobile.android.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:73)                                                                                                    	at asia.zeals.mobile.android.MainActivity$onCreate$1$1$1$1.invoke(MainActivity.kt:72)                                                                                                    	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)                                                                                                    	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
when using encrypted datastore library:
<https://github.com/osipxd/encrypted-datastore>
, but not when I use regular, unencrypted datastore?
The commented out code doesn’t crash, but the uncommented one does
Copy code
//        PreferenceDataStoreFactory.create(
//            corruptionHandler = ReplaceFileCorruptionHandler(
//                produceNewData = { emptyPreferences() }
//            ),
//            migrations = listOf(SharedPreferencesMigration(androidContext(), "settings")),
//            scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob()),
//            produceFile = { androidContext().preferencesDataStoreFile("settings") }
//        )

        val context = androidContext()
        PreferenceDataStoreFactory.createEncrypted(
            corruptionHandler = ReplaceFileCorruptionHandler(
                produceNewData = { emptyPreferences() }
            ),
            migrations = listOf(SharedPreferencesMigration(context, "settings")),
            scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob()),
            produceFile = {
                EncryptedFile(
                context = context,
                file = context.preferencesDataStoreFile("settings.preferences_pb"),
                masterKey = MasterKey(context)
            )
            }
        )
s
This error usually means that you have created a
mutableStateOf
in composition, but tried to use it in background thread before composition was completed
m
Copy code
class AppStateViewModel(
    private val repository: Repository
):ViewModel() {

    var screenState by mutableStateOf(ScreenState.LOADING)
}
From the stack trace, the screenState variable above is what’s causing it. I created it in a ViewModel though. And it’s initialized
j
Hi Andrei, we experienced this exception when mutating state in VM from a bg thread. Is that disallowed?
Copy code
class MyVM {
     val state = mutableStateOf(1)

     init { 
          viewModelScope.launch(Dispatchers.Default) {
              // heavy suspeding work
              state.value = 2
          }
     }
}
switching the dsipatcher to Main helps. Alternatively, was there a development, i.e. we use a version year old, maybe they were some fixes. Thx
(VM is created in composition, so it could match your "description")
s
Writing to state should be fine, I don't think this is the same crash
👍 1
I think there was a fix that allowed this at some point in 1.7 lifecycle
👍 1
j
s
Yeah, that's the change I am referring to. Global snapshot usually means anything outside of composition, so thats what is happening here.
👍 1
j
Thx a lot.