更新 (2020-05-27):
更符合 Kotlin 语言惯用方式的一种方法,使用 Flow API,并借鉴 Juan 的答案,可以表示为以下独立函数:
inline fun <ResultType, RequestType> networkBoundResource(
crossinline query: () -> Flow<ResultType>,
crossinline fetch: suspend () -> RequestType,
crossinline saveFetchResult: suspend (RequestType) -> Unit,
crossinline onFetchFailed: (Throwable) -> Unit = { Unit },
crossinline shouldFetch: (ResultType) -> Boolean = { true }
) = flow<Resource<ResultType>> {
emit(Resource.Loading(null))
val data = query().first()
val flow = if (shouldFetch(data)) {
emit(Resource.Loading(data))
try {
saveFetchResult(fetch())
query().map { Resource.Success(it) }
} catch (throwable: Throwable) {
onFetchFailed(throwable)
query().map { Resource.Error(throwable, it) }
}
} else {
query().map { Resource.Success(it) }
}
emitAll(flow)
}
以上代码可以从一个类(例如Repository)中调用,方法如下:
fun getItems(request: MyRequest): Flow<Resource<List<MyItem>>> {
return networkBoundResource(
query = { dao.queryAll() },
fetch = { retrofitService.getItems(request) },
saveFetchResult = { items -> dao.insert(items) }
)
}
Translated answer:
我一直使用livedata-ktx
工件来实现,不需要传入任何CoroutineScope。该类还仅使用一种类型,而不是两种类型(例如ResultType/RequestType),因为我总是在其他地方使用适配器来映射它们。
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.map
import nihk.core.Resource
abstract class NetworkBoundResource<T> {
fun asLiveData() = liveData<Resource<T>> {
emit(Resource.Loading(null))
if (shouldFetch(query())) {
val disposable = emitSource(queryObservable().map { Resource.Loading(it) })
try {
val fetchedData = fetch()
disposable.dispose()
saveFetchResult(fetchedData)
emitSource(queryObservable().map { Resource.Success(it) })
} catch (e: Exception) {
onFetchFailed(e)
emitSource(queryObservable().map { Resource.Error(e, it) })
}
} else {
emitSource(queryObservable().map { Resource.Success(it) })
}
}
abstract suspend fun query(): T
abstract fun queryObservable(): LiveData<T>
abstract suspend fun fetch(): T
abstract suspend fun saveFetchResult(data: T)
open fun onFetchFailed(exception: Exception) = Unit
open fun shouldFetch(data: T) = true
}
就像 @CommonsWare 在评论中提到的那样,但是,直接暴露一个 Flow<T>
会更好。以下是我尝试用来实现这一点的代码。请注意,我尚未在生产中使用此代码,所以买家自负。
import kotlinx.coroutines.flow.*
import nihk.core.Resource
abstract class NetworkBoundResource<T> {
fun asFlow(): Flow<Resource<T>> = flow {
val flow = query()
.onStart { emit(Resource.Loading<T>(null)) }
.flatMapConcat { data ->
if (shouldFetch(data)) {
emit(Resource.Loading(data))
try {
saveFetchResult(fetch())
query().map { Resource.Success(it) }
} catch (throwable: Throwable) {
onFetchFailed(throwable)
query().map { Resource.Error(throwable, it) }
}
} else {
query().map { Resource.Success(it) }
}
}
emitAll(flow)
}
abstract fun query(): Flow<T>
abstract suspend fun fetch(): T
abstract suspend fun saveFetchResult(data: T)
open fun onFetchFailed(throwable: Throwable) = Unit
open fun shouldFetch(data: T) = true
}
suspend
函数或返回Channel
/Flow
对象,具体取决于API的性质。实际上,协程是在ViewModel中设置的。LiveData
由ViewModel引入,而不是由代码库引入。 - CommonsWareNetworkBoundResource
的人。我的评论更为一般:在我看来,Kotlin仓库实现应该暴露与协程相关的API。 - CommonsWareLiveData
缺乏RxJava或Kotlin协程的能力。LiveData
非常适用于与活动或片段的“最后一英里”通信,并考虑到了这一点而设计。对于小型应用程序,如果您想跳过存储库并使ViewModel
直接与RoomDatabase
通信,则LiveData
是可以的。 - CommonsWare