如何解决从 ViewModel 中的 SqlDelight 收集流时出现 java.lang.IllegalStateException
我正在尝试在我的应用中使用 SqlDelight 数据库。
在我的 DAO 中,我有一个名为 getRecipeById 的函数来查询数据库并返回域模型流(Recipe)。下面是函数的实现:(注意:RecipeTable 是表的名字,或者我想我应该叫它 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$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:133)
at com.noat.recipe_food2fork.data.repositoryImplementation.recipeRepository.RecipeRepository$getRecipeById$$inlined$map$1$2.emit(Collect.kt:135)
at com.noat.recipe_food2fork.data.local.RecipeLocalDatabase$getRecipeById$$inlined$map$1$2.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map$1$2.emit(Collect.kt:135)
at com.squareup.sqldelight.runtime.coroutines.FlowQuery$mapToOne$$inlined$map$1$2$1.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)
我认为是流收集导致了错误,或者可能是因为我使用的是 MutableState?谷歌搜索错误没有多大帮助......有谁知道这个问题的原因是什么?谢谢!抱歉长篇大论。
解决方法
我不认为 MutableState
旨在用于 ViewModel 层,因为它是与 compose 运行时集成的可观察对象。
您可以改为创建 MutableStateFlow
并使用视图层中的 collectAsState()
。
在您的情况下,问题可能是,因为状态是在组合外调用的协程中捕获的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。