gpt4 book ai didi

mvvm - 尽管正在观察MediatorLiveData onChanged,但未调用

转载 作者:行者123 更新时间:2023-12-03 10:41:21 24 4
gpt4 key购买 nike

在NetworkBoundResource文件中,直到行“Log.d(TAG,” init:named“)”起作用,但是
尽管我在RestaurantViewModel类中进行了观察,但results.addSource无法正常工作,而在Activity中也观察到了另一个在NetworkBoundResource中观察结果的MediatorLiveData。
如果我运行该应用程序,则不会发生任何事件。没错显然缺少了一些东西,但是我找不到它。任何答案都将真正有帮助。预先谢谢你。
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)
}
}
}
}
中的subscriptionObservers函数RestaurantActivity.kt
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 -> {

}
}
}
})
}
中的searchByRestaurantId函数RestaurantRepository.kt
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
}
NetworkBoundResource.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)) }
}
}
}

最佳答案

哦,问题解决了。在收到apiResponse之前,我从mediatorLivedata中删除了源。这就是为什么我无法调用onChanged方法的原因。

关于mvvm - 尽管正在观察MediatorLiveData onChanged,但未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64998852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com