- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您对如何使用 NetworkBoundResource 实现存储库模式有任何想法吗?和 Kotlin 协程?我知道我们可以使用 GlobalScope 启动协程,但这可能会导致协程泄漏。我想将 viewModelScope 作为参数传递,但在实现时有点棘手(因为我的存储库不知道任何 ViewModel 的 CoroutineScope)。
abstract class NetworkBoundResource<ResultType, RequestType>
@MainThread constructor(
private val coroutineScope: CoroutineScope
) {
private val result = MediatorLiveData<Resource<ResultType>>()
init {
result.value = Resource.loading(null)
@Suppress("LeakingThis")
val dbSource = loadFromDb()
result.addSource(dbSource) { data ->
result.removeSource(dbSource)
if (shouldFetch(data)) {
fetchFromNetwork(dbSource)
} else {
result.addSource(dbSource) { newData ->
setValue(Resource.success(newData))
}
}
}
}
@MainThread
private fun setValue(newValue: Resource<ResultType>) {
if (result.value != newValue) {
result.value = newValue
}
}
private fun fetchFromNetwork(dbSource: LiveData<ResultType>) {
val apiResponse = createCall()
result.addSource(dbSource) { newData ->
setValue(Resource.loading(newData))
}
result.addSource(apiResponse) { response ->
result.removeSource(apiResponse)
result.removeSource(dbSource)
when (response) {
is ApiSuccessResponse -> {
coroutineScope.launch(Dispatchers.IO) {
saveCallResult(processResponse(response))
withContext(Dispatchers.Main) {
result.addSource(loadFromDb()) { newData ->
setValue(Resource.success(newData))
}
}
}
}
is ApiEmptyResponse -> {
coroutineScope.launch(Dispatchers.Main) {
result.addSource(loadFromDb()) { newData ->
setValue(Resource.success(newData))
}
}
}
is ApiErrorResponse -> {
onFetchFailed()
result.addSource(dbSource) { newData ->
setValue(Resource.error(response.errorMessage, newData))
}
}
}
}
}
}
最佳答案
更新(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)
}
上面的代码可以从类中调用,例如一个存储库,像这样:
fun getItems(request: MyRequest): Flow<Resource<List<MyItem>>> {
return networkBoundResource(
query = { dao.queryAll() },
fetch = { retrofitService.getItems(request) },
saveFetchResult = { items -> dao.insert(items) }
)
}
原答案:
这就是我使用 livedata-ktx
所做的事情 Artifact ;无需传入任何 CoroutineScope。该类也只使用一种类型而不是两种(例如 ResultType/RequestType),因为我最终总是在其他地方使用适配器来映射它们。
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.map
import nihk.core.Resource
// Adapted from: https://developer.android.com/topic/libraries/architecture/coroutines
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()
// Stop the previous emission to avoid dispatching the saveCallResult as `Resource.Loading`.
disposable.dispose()
saveFetchResult(fetchedData)
// Re-establish the emission as `Resource.Success`.
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
}
关于android - 带 Kotlin 协程的 NetworkBoundResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58486364/
我正在使用 Retrofit、LiveData、Room (Android AAC)。 NetworkBoundResource是googlesimple提供的网络资源和房间结合的好 helper .
在 Google NetworkBoundResource 类中,我无法理解 MediatorLiveData 的 addSource() 和 removeSource() 的顺序被使用。 例如--在
您对如何使用 NetworkBoundResource 实现存储库模式有任何想法吗?和 Kotlin 协程?我知道我们可以使用 GlobalScope 启动协程,但这可能会导致协程泄漏。我想将 vie
我遇到了一些有线问题。当我第一次订阅时,它会进行网络调用并将数据保存到数据库,但是 loadFromDb() 永远不会执行,并且不会引发任何错误。 为什么会发生这种情况? Flowable>>
我的应用使用 Android 的 Architecture components库并显示从具有无限滚动效果的分页 REST api 获取的项目列表。 我想做的是使用 Paging Library结合
当我尝试为 Room Db 和 Retrofit 实现 NetworkBoundResource 和 Resource 帮助程序类时,它工作得很好。但是,我需要使用 Retrofit only wit
Google的android架构组件教程here有一部分解释了如何抽象化通过网络获取数据的逻辑。在其中,他们创建了一个名为 NetworkBoundResource 的抽象类,使用 LiveData
我是一名优秀的程序员,十分优秀!