直到另一个操作完成?

问题描述

我在我的 Android 应用程序中使用以下代码将数据从远程源加载到本地数据库缓存中。

inline fun <ResultType,RequestType> networkBoundResource(
    crossinline query: () -> Flow<ResultType>,crossinline fetch: suspend () -> RequestType,crossinline saveFetchResult: suspend (RequestType) -> Unit,crossinline onFetchSuccess: () -> Unit = { },crossinline onFetchFailed: (Throwable) -> Unit = { },crossinline shouldFetch: (ResultType) -> Boolean = { true }
) = flow {
    val data = query().first()

    val flow = if (shouldFetch(data)) {
        emit(Resource.(data))

        try {
            // this Could take a while,I want to keep getting updates meanwhile
            saveFetchResult(fetch())
            onFetchSuccess()
            query().map { Resource.Success(it) }
        } catch (t: Throwable) {
            onFetchFailed(t)
            query().map { Resource.Error(t,it) }
        }
    } else {
        query().map { Resource.Success(it) }
    }
    emitAll(flow)
}

query一个数据库查询,它通过 emitAll 不断发出数据库更新,直到我们再次调用方法

此设置的问题在于 Resource.Loading 仅包含当前数据 (first()) 的“快照”,并且在到达 try/catch 的末尾之前我们不会收到任何数据库更新{1}} 阻止并调用 emitAll。但我想在 Loading 仍在进行中时继续接收数据库更新。但是,我不能只在 emitAll调用 Resource.Loading,因为它会阻塞整个 Flow。

有没有办法在 emitAll调用 Loading,然后在 Success 块完成后切换到 Error/try

解决方法

我只做了简单的测试来验证它,但看起来您可以监听查询并根据外部 Flow 的上下文发出它在新启动的协程中传播的任何/所有数据 - 其他工作在函数中将继续,畅通无阻。完成其他工作后,可以取消正在侦听查询的协程。例如:

inline fun <ResultType,RequestType> networkBoundResource(
    crossinline query: () -> Flow<ResultType>,crossinline fetch: suspend () -> RequestType,crossinline saveFetchResult: suspend (RequestType) -> Unit,crossinline onFetchFailed: (Throwable) -> Unit = { },crossinline shouldFetch: (ResultType) -> Boolean = { true }
): Flow<Resource<ResultType>> = flow {
    emit(Resource.Loading())
    val data = query().first()

    val flow = if (shouldFetch(data)) {
        val flowContext = currentCoroutineContext()
        val loading: Job = coroutineScope {
            launch(flowContext) {
                query().map { Resource.Loading(it) }
                    .collect { withContext(flowContext) { emit(it) } }
            }
        }

        try {
            val request = fetch()
            loading.cancel()
            saveFetchResult(request)
            query().map { Resource.Success(it) }
        } catch (throwable: Throwable) {
            loading.cancel()
            onFetchFailed(throwable)
            query().map { Resource.Error(throwable,it) }
        }
    } else {
        query().map { Resource.Success(it) }
    }

    emitAll(flow)
}

让我知道这是否有效!