java.lang.IllegalStateException 在 ViewModel 中从 SqlDelight 收集流量时
java.lang.IllegalStateException when collecting flow from SqlDelight in ViewModel
我正在尝试在我的应用程序中使用 SqlDelight 数据库。
在我的 DAO 中,我有一个名为 getRecipeById 的函数来查询数据库和 return 一个域模型流(Recipe)。下面是该函数的实现:(注意:RecipeTable 是 table 的名称,或者我想我应该叫它 RecipeEntity)
fun getRecipeById(id: Int): Flow<Recipe> {
return recipeQueries.getRecipeById(id)
.asFlow()
.mapToOne()
.map { recipeTable: RecipeTable ->
recipeTableToRecipeMapper(recipeTable = recipeTable)
}
}
然后我的存储库像这样调用这个函数:(注意:recipeLocalDatabase 是我的 DAO)
suspend fun getRecipeById(id: Int): Flow<RecipeDTO> {
val recipeFromDB: Flow<Recipe> = recipeLocalDatabase.getRecipeById(id)
return recipeFromDB.map { recipe: Recipe ->
recipeDTOMapper.mapDomainModelToDTO(recipe)
}
}
然后我的用例层调用存储库中的函数并传递流程:
suspend fun execute(
id: Int
): UseCaseResult<Flow<RecipeDTO>>
{
return try{
UseCaseResult.Success(recipeRepository.getRecipeById(id))
} catch(exception: Exception){
UseCaseResult.Error(exception)
}
}
现在在 ViewModel 中,我正在像这样收集 RecipeDTO 的流程:
var recipeForDetailView: MutableState<RecipeDTO> = mutableStateOf(
RecipeDTO(
id = 0,
title = "",
featuredImage = "",
ingredients = listOf()
)
)
fun onLaunch(recipeId: Int){
viewModelScope.launch(Dispatchers.IO) {
when(val result = getRecipeDetailUseCase.execute(recipeId))
{
is UseCaseResult.Success ->
result.resultValue.collect{ recipeDTO: RecipeDTO ->
recipeForDetailView.value = recipeDTO
}
is UseCaseResult.Error -> Log.d("Debug: RecipeDetailViewModel",
result.exception.toString()
)
}
}
}
然后我的视图层将观察 MutableState 对象。
现在,我每隔一段时间就会收到这个错误
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-5
Process: com.noat.recipe_food2fork, PID: 13923
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:1530)
at androidx.compose.runtime.snapshots.SnapshotKt.current(Snapshot.kt:1770)
at androidx.compose.runtime.SnapshotMutableStateImpl.setValue(SnapshotState.kt:891)
at com.noat.recipe_food2fork.ui.viewModel.RecipeDetailViewModel$onLaunch$invokeSuspend$$inlined$collect.emit(Collect.kt:133)
at com.noat.recipe_food2fork.data.repositoryImplementation.recipeRepository.RecipeRepository$getRecipeById$$inlined$map.emit(Collect.kt:135)
at com.noat.recipe_food2fork.data.local.RecipeLocalDatabase$getRecipeById$$inlined$map.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map.invokeSuspend(Unknown Source:12)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
我觉得是流量采集导致的错误,或者是因为我使用的是Mutable状态?谷歌搜索错误并没有太大帮助......有谁知道这个问题的原因是什么?谢谢!对不起,很长很长 post.
我不认为 MutableState
设计用于 ViewModel 层,因为它是与 compose 运行时集成的可观察对象。
您可以改为创建 MutableStateFlow
并使用视图层中的 collectAsState()
。
在您的情况下,问题可能是因为状态是在组合外部调用的协程中捕获的。
正如@Robert Nagy 所建议的。在 Dispatchers.Main 上下文中设置可变状态值或在视图模型中使用 MutableStateFlow 并在可组合函数中使用 collectAsState()。
private fun getExpenseAddData() {
_expenseAddState.value = ExpenseAddState(isLoading = true)
viewModelScope.launch(Dispatchers.IO) {
val expenseAddState = try {
val categoryListDto = repository.getAllCategories()
val paymentTypeListDto = repository.getAllPaymentType()
if (categoryListDto.isNullOrEmpty() || paymentTypeListDto.isNullOrEmpty()) {
ExpenseAddState(error = "Error retrieving expense category or payment type")
} else {
ExpenseAddState(expenseAddData = ExpenseAddData(
categoryList = categoryListDto.map { it.toExpenseCategory() },
paymentTypeList = paymentTypeListDto.map { it.toPaymentType() }
))
}
} catch (e: Exception) {
ExpenseAddState(error = e.message
?: "Error retrieving expense category or payment type")
}
withContext(Dispatchers.Main) {
_expenseAddState.value = expenseAddState
}
}
}
我正在尝试在我的应用程序中使用 SqlDelight 数据库。
在我的 DAO 中,我有一个名为 getRecipeById 的函数来查询数据库和 return 一个域模型流(Recipe)。下面是该函数的实现:(注意:RecipeTable 是 table 的名称,或者我想我应该叫它 RecipeEntity)
fun getRecipeById(id: Int): Flow<Recipe> {
return recipeQueries.getRecipeById(id)
.asFlow()
.mapToOne()
.map { recipeTable: RecipeTable ->
recipeTableToRecipeMapper(recipeTable = recipeTable)
}
}
然后我的存储库像这样调用这个函数:(注意:recipeLocalDatabase 是我的 DAO)
suspend fun getRecipeById(id: Int): Flow<RecipeDTO> {
val recipeFromDB: Flow<Recipe> = recipeLocalDatabase.getRecipeById(id)
return recipeFromDB.map { recipe: Recipe ->
recipeDTOMapper.mapDomainModelToDTO(recipe)
}
}
然后我的用例层调用存储库中的函数并传递流程:
suspend fun execute(
id: Int
): UseCaseResult<Flow<RecipeDTO>>
{
return try{
UseCaseResult.Success(recipeRepository.getRecipeById(id))
} catch(exception: Exception){
UseCaseResult.Error(exception)
}
}
现在在 ViewModel 中,我正在像这样收集 RecipeDTO 的流程:
var recipeForDetailView: MutableState<RecipeDTO> = mutableStateOf(
RecipeDTO(
id = 0,
title = "",
featuredImage = "",
ingredients = listOf()
)
)
fun onLaunch(recipeId: Int){
viewModelScope.launch(Dispatchers.IO) {
when(val result = getRecipeDetailUseCase.execute(recipeId))
{
is UseCaseResult.Success ->
result.resultValue.collect{ recipeDTO: RecipeDTO ->
recipeForDetailView.value = recipeDTO
}
is UseCaseResult.Error -> Log.d("Debug: RecipeDetailViewModel",
result.exception.toString()
)
}
}
}
然后我的视图层将观察 MutableState 对象。 现在,我每隔一段时间就会收到这个错误
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-5
Process: com.noat.recipe_food2fork, PID: 13923
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:1530)
at androidx.compose.runtime.snapshots.SnapshotKt.current(Snapshot.kt:1770)
at androidx.compose.runtime.SnapshotMutableStateImpl.setValue(SnapshotState.kt:891)
at com.noat.recipe_food2fork.ui.viewModel.RecipeDetailViewModel$onLaunch$invokeSuspend$$inlined$collect.emit(Collect.kt:133)
at com.noat.recipe_food2fork.data.repositoryImplementation.recipeRepository.RecipeRepository$getRecipeById$$inlined$map.emit(Collect.kt:135)
at com.noat.recipe_food2fork.data.local.RecipeLocalDatabase$getRecipeById$$inlined$map.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map.invokeSuspend(Unknown Source:12)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
我觉得是流量采集导致的错误,或者是因为我使用的是Mutable状态?谷歌搜索错误并没有太大帮助......有谁知道这个问题的原因是什么?谢谢!对不起,很长很长 post.
我不认为 MutableState
设计用于 ViewModel 层,因为它是与 compose 运行时集成的可观察对象。
您可以改为创建 MutableStateFlow
并使用视图层中的 collectAsState()
。
在您的情况下,问题可能是因为状态是在组合外部调用的协程中捕获的。
正如@Robert Nagy 所建议的。在 Dispatchers.Main 上下文中设置可变状态值或在视图模型中使用 MutableStateFlow 并在可组合函数中使用 collectAsState()。
private fun getExpenseAddData() {
_expenseAddState.value = ExpenseAddState(isLoading = true)
viewModelScope.launch(Dispatchers.IO) {
val expenseAddState = try {
val categoryListDto = repository.getAllCategories()
val paymentTypeListDto = repository.getAllPaymentType()
if (categoryListDto.isNullOrEmpty() || paymentTypeListDto.isNullOrEmpty()) {
ExpenseAddState(error = "Error retrieving expense category or payment type")
} else {
ExpenseAddState(expenseAddData = ExpenseAddData(
categoryList = categoryListDto.map { it.toExpenseCategory() },
paymentTypeList = paymentTypeListDto.map { it.toPaymentType() }
))
}
} catch (e: Exception) {
ExpenseAddState(error = e.message
?: "Error retrieving expense category or payment type")
}
withContext(Dispatchers.Main) {
_expenseAddState.value = expenseAddState
}
}
}