1

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

然后,我的视图层将观察 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?谷歌搜索错误没有多大帮助......有谁知道这个问题的原因是什么?谢谢!对不起,很长的帖子。

4

2 回答 2

3

我不认为MutableState它是为在 ViewModel 层中使用而设计的,因为它是与 compose 运行时集成的 observable。您可以创建一个MutableStateFlowcollectAsState()从视图层使用。

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

于 2021-06-14T06:21:42.923 回答
0

正如@Robert Nagy 所建议的那样。在 Dispatchers.Main 上下文中设置可变状态值或在 viewmodel 中使用 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
        }
    }
}
于 2021-09-20T14:05:20.827 回答