- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下用于搜索功能的 UI。
View (UI) << ViewModel() << LiveData(Remote)
(Search UI) (Search VM) Fetch data from remote
如上所述,View 观察 ViewModel 中的一个特定方法,该方法返回如下 LiveData:
override fun onStart() {
super.onStart()
viewModel.finalLiveData.observe(viewLifecycleOwner, listChangeObserver)
}
// Observer responsible for updating the UI via adapter.
private val listChangeObserver: Observer<List<User>> = Observer { users ->
users?.let { updateUI(it) } ?: run { updateUI(emptyList()) }
}
override fun onStop() {
viewModel.finalLiveData.removeObserver(listChangeObserver)
super.onStop()
}
在 ViewModel 中,initSearch(searchKey:String)
过滤从远程接收的 LiveData 并为 View 准备最后一个:
// Initiates the search with supplied search keys.
fun initSearch(searchInput: String?) {
// filtering happens in Deserializer() class
finalLiveData = Transformations.map(FirebaseQueryLiveData(query)) {
repository.getSearchList(it, searchInput, searchLocation)
}
}
并且 initSearch
将作为 viewModel.initSearch(searchKey)
从 View 中调用。现在的问题是,finalLiveData
从转换中接收到值,但不幸的是 View 没有更新。
但是,注意到如果用户尝试最近的应用程序并返回(通过调用 onPause()
和 onResume()
),数据会反射(reflect)在 View 中
有没有更好的方法来更新观察到的LiveData
?提前致谢。
最佳答案
userListRemote
是您存储库中的 LiveData
吗?如果没有 lifecycleowner
,它将无法工作。
您可以尝试直接映射到您的 finalLiveData
而不是使用 temp
:
finalLiveData = Transformations.map(userListRemote, // do your mapping here)
这样,userListRemote
就会使用finalLiveData
的viewLifecycleOwner
。
关于android - LiveData 未在 postValue() 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089440/
我有以下用于搜索功能的 UI。 View (UI) > = Observer { users -> users?.let { updateUI(it) } ?: run { upda
我想知道如何从完成的 ASIHTTPRequest 中检索发布值。 当我执行请求时,我会这样做: [request setPostValue:uniqueID forKey:@"bookUniqueI
我正在实现 Mvvm 模型并尝试使用 MutableLiveData 来监听变化。 有两种方法 setValue() - 设置值。如果有活跃的观察者,值将被分派(dispatch)给他们。这个方法必须
As the document says about the Livedata#postValue(T) : If you called this method multiple times befo
MutableLiveData 的改变值有两种方式。但是 MutableLiveData 中的 setValue() 和 postValue() 有什么区别。 我找不到相同的文档。 这里是Androi
在OnCreate中执行 public void Start() { ModelSingleHotTrendingNews modelSingleHotTrendingNews
我尝试在我的应用程序上测试一些方法,但在调用 mutablelivedata.postValue 时出现错误。这是一个 fragment 和错误消息: @Test public void callSt
我正在尝试使用实时数据为 View 模型编写单元测试。 LoginViewModel.kt class LoginViewModel @Inject constructor( val cont
LiveData setValue 应该已经触发了 Activity 中的 onChanged 方法,但是它只在第一次调用,在我尝试进行分页之后,它中断并且不再调用 onChanged,尽管我的响应确
我是一名优秀的程序员,十分优秀!