Mario Andhika
08/10/2023, 7:22 AMjava.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?Mario Andhika
08/10/2023, 7:29 AM// 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)
)
}
)
shikasd
08/10/2023, 5:52 PMmutableStateOf
in composition, but tried to use it in background thread before composition was completedMario Andhika
08/11/2023, 2:23 AMclass 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 initializedJan Skrasek
09/16/2024, 4:12 PMclass 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.
ThxJan Skrasek
09/16/2024, 4:13 PMshikasd
09/16/2024, 4:14 PMshikasd
09/16/2024, 4:15 PMJan Skrasek
09/16/2024, 4:16 PMshikasd
09/16/2024, 4:17 PMJan Skrasek
09/16/2024, 4:19 PM