【发布时间】:2020-08-02 11:56:59
【问题描述】:
我的代码有一个奇怪的问题。
场景...我有一个数据列表,我在 Viewmodel 中检索它们并从 Fragment 观察。当我第一次打开我的 Fragment 时,一切正常。但是当我从另一个片段返回时,LiveData 不为空。所以我这样检查,
if(liveData.getUser().getValue()!= null){
//here getName() does not null and I get the value from liveData
println(liveData.getUser().getName()) //this work
liveData.getUser().observe(lifeCycleOwner(),
Observer { user: User? ->{
//Here LiveData is null
}
})
}
else{
liveData.initiate()
LiveData.getUser.observe(//same as above)
}
我在 onViewCreated() 中观察。我不知道为什么 LiveData.getValue() 不为 null 但 LiveData.observe 返回 null。
有人可以帮我解决这个问题吗?
编辑...这是我的UserViewmodel.class
class UserViewModel : ViewModel(){
private var userLiveData : LiveData<User>?= MutableLiveData<>()
fun initiate (){
userLiveData = UserRepository.getUserData()
}
fun getUser() : LiveData<User>{
Log.e("get called")
return userLiveData
}
}
编辑 2 这是我的 UserRepository.class
class UserRepository {
private var userData : MutableLiveData<User>?= null
fun getUserData(): LiveData<User>{
retrieveUser()
return userData
}
fun retrieveUser (){
val names = arrayOf("user1","user2","user3","user4","user5")
for(name :String in names){
user = User()
user.setName(name)
userData.value = user
}
}
}
【问题讨论】:
-
在这里发布视图模型
-
我已添加,请再次查看。
-
你不需要在
if else观察,一次就可以了,顺便说一下UserRepository -
我已添加先生。如果我不使用 if else ,则从另一个 Fragment 返回会使最后一次观察到的值调用两次。即最后观察到的值在返回时加倍。
标签: android kotlin android-livedata android-architecture-components