我正在尝试在我的应用程序中使用 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?谷歌搜索错误没有多大帮助......有谁知道这个问题的原因是什么?谢谢!对不起,很长的帖子。