在 NetworkBoundResource 文件中,直到 "Log.d(TAG, "init: called")" 行起作用,但是 results.addSource 不起作用,尽管我在我的 RestaurantViewModel 类中观察它,而另一个 MediatorLiveData 在 NetworkBoundResource 中观察结果在我的活动中被观察到。
如果我运行该应用程序,则不会发生任何事件。没有错误。很明显,缺少某些东西,但是我找不到它是什么。任何答案都会非常有帮助。提前谢谢你。
RestaurantViewModel.kt
class RestaurantViewModel(application: Application): AndroidViewModel(application) {
companion object {
private const val TAG = "RestaurantViewModel"
}
class Factory(private val application: Application) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return RestaurantViewModel(application) as T
}
}
private val mRestaurantRepository: RestaurantRepository = RestaurantRepository.instance(application)
private val results: MediatorLiveData<Resource<RestaurantDetail?>?> = MediatorLiveData()
val restaurantDetail: MediatorLiveData<Resource<RestaurantDetail?>?>
get() = results
fun searchByRestaurantId(resId: Int) {
executeSearch(resId)
}
private fun executeSearch(resId: Int) {
val repositorySource = mRestaurantRepository.searchByRestaurantId(resId)
results.addSource(repositorySource) { detailResource ->
if(detailResource != null) {
Log.d(TAG, "executeSearch: $detailResource")
results.value = detailResource
results.removeSource(repositorySource)
} else {
results.removeSource(repositorySource)
}
}
}
}
RestaurantActivity.kt中的 subscribeObservers 函数
private fun subscribeObservers() {
mRestaurantViewModel?.restaurantDetail?.observe(this, Observer { detailResource ->
if (detailResource != null) {
when (detailResource.status) {
Resource.Status.SUCCESS -> {
detailResource.data?.let { restaurantDetail ->
Log.d(TAG, "subscribeObservers: $restaurantDetail")
setRestaurantProperties(restaurantDetail)
}
}
Resource.Status.ERROR -> {
}
Resource.Status.LOADING -> {
}
}
}
})
}
RestaurantRepository.kt中的 searchByRestaurantId 函数
fun searchByRestaurantId(resId: Int): LiveData<Resource<RestaurantDetail?>?> {
return object: NetworkBoundResource<RestaurantDetail, RestaurantResponse>() {
override fun saveCallResult(item: RestaurantResponse) {
Log.d(TAG, "saveCallResult: called")
item.getRestaurant?.let { restaurantDetail ->
Log.d(TAG, "saveCallResult: $restaurantDetail")
restaurantDao!!.insertRestaurant(restaurantDetail)
}
}
override fun shouldFetch(data: RestaurantDetail?): Boolean {
return true
}
override fun loadFromDb(): LiveData<RestaurantDetail?> {
Log.d(TAG, "loadFromDb: called")
return restaurantDao!!.searchByRestaurantId(resId)
}
override fun createCall(): LiveData<ApiResponse<RestaurantResponse?>?> {
Log.d(TAG, "createCall: called")
val apiResponse = ServiceGenerator.retrofitService.searchByRestaurantId(resId)
Log.d(TAG, "createCall: $apiResponse")
return apiResponse
}
}.asLiveData
}
网络绑定资源.kt
abstract class NetworkBoundResource<CacheObject, RequestObject> {
companion object {
private val TAG: String? = "NetworkBoundResource"
}
private var results: MediatorLiveData<Resource<CacheObject?>?> = MediatorLiveData()
init {
init()
Log.d(TAG, "NetworkBoundResource: called")
}
fun setValue(newValue: Resource<CacheObject?>?) {
if (results.value !== newValue) {
results.value = newValue
}
}
private fun init() {
// update LiveData for loading status
results.value = loading(null)
// observe LiveData source from local db
val dbSource: LiveData<CacheObject?> = loadFromDb()
Log.d(TAG, "init: called")
results.addSource(dbSource) { cacheObject ->
results.removeSource(dbSource)
if (shouldFetch(cacheObject)) {
// get data from the network
fetchFromNetwork(dbSource)
} else {
results.addSource(
dbSource
) { cacheObject -> setValue(Resource.success(cacheObject)) }
}
}
}