android - 在 ViewModel 中从 SqlDelight 收集流时出现 java.lang.IllegalStateException

标签 android kotlin-coroutines kotlin-flow sqldelight

我正在尝试在我的应用程序中使用 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()
            )
        }
    }
}

然后我的 View 层将观察 Mu​​tableState 对象。 现在,我每隔一段时间就会收到这个错误

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 并从 View 层使用 collectAsState()

在您的情况下,问题可能是因为状态是在组合外部调用的协程中捕获的。

关于android - 在 ViewModel 中从 SqlDelight 收集流时出现 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67963766/

相关文章:

android - 数据绑定(bind) : Cannot bind ArrayAdpater to AppCompatAutoCompleteTextView

android - 使用协程进行多次循环调用

multithreading - Kotlin 协程中的可见性

android - 使用 Kotlin StateFlow 的 TextField

android - Kotlin Map 项目列表流

android - 如果手机可以上网,我们如何得到通知?

java - 后台线程与UI交互

android静音相机快门声音?

android - 如何将协程范围限定为 fragment ,以便在 fragment 离开屏幕或被销毁时自动取消?

android - 在 Kotlin 协程中使用流替换 RxJava 的点击监听器