Android 可以对ViewModel内的Transformations.map()返回的Livedata调用observeForRever()吗?
我尝试使用LiveData Transformations.map()检查结果并更新UI。但是在没有观察者的情况下不会触发Transformations.map()回调 那么,对transformations.map返回的livedata调用Android 可以对ViewModel内的Transformations.map()返回的Livedata调用observeForRever()吗?,android,android-architecture-components,android-livedata,android-viewmodel,android-mvvm,Android,Android Architecture Components,Android Livedata,Android Viewmodel,Android Mvvm,我尝试使用LiveData Transformations.map()检查结果并更新UI。但是在没有观察者的情况下不会触发Transformations.map()回调 那么,对transformations.map返回的livedata调用observeForver{}是一种好方法吗?并删除onCleared on ViewModel上的OBerver? private lateinit var map: LiveData<Unit> fun getAppConfigur
observeForver{}
是一种好方法吗?并删除onCleared on ViewModel上的OBerver?
private lateinit var map: LiveData<Unit>
fun getAppConfiguration(): MutableLiveData<TopRatedMoviesResponse> {
progressDialogVisibleLiveData.postValue(true)
val appConfigurationLiveData = MutableLiveData<TopRatedMoviesResponse>()
val appConfigurationSourceLiveData : MutableLiveData<DataResult> = splashScreenRepository.getAppConfiguration(getApplication())
map = Transformations.map(appConfigurationSourceLiveData) { dataResult ->
progressDialogVisibleLiveData.postValue(false)
when (dataResult) {
is DataResultSuccess -> {
appConfigurationLiveData.postValue(dataResult.data as TopRatedMoviesResponse)
}
is DataResultFailed -> {
when (dataResult.errorCode) {
HTTPError.NO_INTERNET -> {
errorDialogVisibleLiveData.postValue(dataResult)
}
HTTPError.BAD_REQUEST -> {
errorDialogVisibleLiveData.postValue(dataResult)
}
HTTPError.UNAUTHORISED -> {
unAuthorisedEventLiveData.postValue(true)
}
HTTPError.FORBIDDEN, HTTPError.NOT_FOUND, HTTPError.INTERNAL_SERVER_ERROR, HTTPError.UNKNOWN -> {
errorDialogVisibleLiveData.postValue(dataResult)
}
}
}
}
}
map.observeForever { }
return appConfigurationLiveData
}
override fun onCleared() {
super.onCleared()
map.removeObserver { }
}
private-lateinit-var映射:LiveData
fun getAppConfiguration():MutableLiveData{
progressDialogVisibleLiveData.postValue(true)
val-appConfigurationLiveData=MutableLiveData()
val-appConfigurationSourceLiveData:MutableLiveData=splashScreenRepository.getAppConfiguration(getApplication())
map=Transformations.map(appConfigurationSourceLiveData){dataResult->
progressDialogVisibleLiveData.postValue(false)
何时(数据结果){
是DataResultAccess->{
appConfigurationLiveData.postValue(dataResult.data作为TopRatedMoviesResponse)
}
是否DataResultFailed->{
何时(dataResult.errorCode){
HTTPError.NO_互联网->{
errorDialogVisibleLiveData.postValue(dataResult)
}
HTTPError.BAD_请求->{
errorDialogVisibleLiveData.postValue(dataResult)
}
HTTPError.unauthorized->{
UnauthorizedEventLiveData.postValue(true)
}
HTTPError.Forbidded,HTTPError.NOT_FOUND,HTTPError.INTERNAL_SERVER_ERROR,HTTPError.UNKNOWN->{
errorDialogVisibleLiveData.postValue(dataResult)
}
}
}
}
}
map.observeForever{}
返回appConfigurationLiveData
}
覆盖有趣的onCleared(){
super.onCleared()
map.removeObserver{}
}
但是,如果没有观察者,则不会触发Transformations.map回调
这是正常的,没有对observerForever()的调用,Transformations.map()返回的LiveData没有任何人提供它所持有的数据
那么,对由返回的livedata调用ObserveForRever{}是一种好方法吗
地图
看看您在该方法中所做的事情,答案是否定的,这不是您使用Transformations.map()的方式。该方法的目标是在将源LiveData发出的值呈现给观察者之前,对这些值应用一些更改。在您的情况下,您需要一个简单的类型更改(从dataResult.data到topratedmoviessresponse),并在出现错误时触发一些error livedata。检查以下代码:
fun getAppConfiguration(): MutableLiveData<TopRatedMoviesResponse> {
progressDialogVisibleLiveData.postValue(true)
val appConfigurationSourceLiveData : MutableLiveData<DataResult> = splashScreenRepository.getAppConfiguration(getApplication())
return Transformations.map(appConfigurationSourceLiveData) { dataResult ->
progressDialogVisibleLiveData.postValue(false)
when (dataResult) {
is DataResultSuccess -> {
dataResult.data as TopRatedMoviesResponse
}
is DataResultFailed -> {
when (dataResult.errorCode) {
HTTPError.UNAUTHORISED -> {
unAuthorisedEventLiveData.postValue(true)
}
HTTPError.FORBIDDEN, HTTPError.NOT_FOUND, HTTPError.INTERNAL_SERVER_ERROR, HTTPError.UNKNOWN, HTTPError.NO_INTERNET, HTTPError.BAD_REQUEST -> {
errorDialogVisibleLiveData.postValue(dataResult)
}
}
// we have to return something even if an error occured
// I'm returning null, your UI should handle this
null
}
}
}
}
fun getAppConfiguration():MutableLiveData{
progressDialogVisibleLiveData.postValue(true)
val-appConfigurationSourceLiveData:MutableLiveData=splashScreenRepository.getAppConfiguration(getApplication())
返回Transformations.map(appConfigurationSourceLiveData){dataResult->
progressDialogVisibleLiveData.postValue(false)
何时(数据结果){
是DataResultAccess->{
dataResult.data作为TopRatedMoviesResponse
}
是否DataResultFailed->{
当(dataResult.errorCode){
HTTPError.unauthorized->{
UnauthorizedEventLiveData.postValue(true)
}
HTTPError.FORBIDDEN,HTTPError.NOT_FOUND,HTTPError.INTERNAL_SERVER_ERROR,HTTPError.UNKNOWN,HTTPError.NO_INTERNET,HTTPError.BAD_请求->{
errorDialogVisibleLiveData.postValue(dataResult)
}
}
//即使发生错误,我们也必须返回一些内容
//我返回null,你的UI应该能处理这个问题
无效的
}
}
}
}
另外,还可以查看处理LiveData数据和错误的其他方法。您应该将lambda存储为字段引用,因为我认为不能保证它是同一个lambda实例。