简体   繁体   English

如何在撰写中观察实时数据?

[英]How to observe livedata in compose?

I need to observe change in list which I am fetching from firebase我需要观察我从 firebase 获取的列表中的变化

viewModel.courseList.observe(viewLifeCycleOwner,{

})

But I'm not able to use viewLifeCycleOwner .但我无法使用viewLifeCycleOwner

class MyViewModel: ViewModel() {
    private var _courseList = MutableLiveData<Whatever>()
    var courseList: LiveData<List<Whatever>> = _courseList
}

@Composable
fun MyComposable() {
    val list  = myViewModel.courseList.observeAsState().value
}

Demo app has plenty of examples: https://github.com/JohannBlake/Jetmagic演示应用程序有很多示例: https : //github.com/JohannBlake/Jetmagic

use subscribe or ko.computed for detect any change in observable variable like使用subscribeko.computed来检测可观察变量的任何变化,例如

viewModel.courseList = ko.observable()
viewModel.courseList.subscribe((changedvalue)=>{
//to do code
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 观察 livedata 并在 jetpack compose 中导航 - Observe livedata and navigate in jetpack compose 如何在 ViewModel 中观察 LiveData? - How to observe on LiveData in ViewModel? 如何在 DialogFragment 中观察 ViewModel LiveData? - How to observe ViewModel LiveData in a DialogFragment? 如何使用 MVVM 观察发射的 LiveData - How to observe Emited LiveData with MVVM 如何观察多个LiveData的状态? - How to observe the states of multiple LiveData? ViewModel中的LiveData如何使用转换观察存储库中的Livedata? - How can LiveData in ViewModel observe the Livedata in Repository using Transformations? 如何在 Android 中以正确的方式通过多次观察来观察实时数据? - How to observe the livedata with multiple observe for correct way in Android? 如何在订阅liveData后仅为新更新观察liveData - how to observe a liveData only for the new update after the subscription to the liveData 如何在 PagedList LiveData 异步提交结果时观察它? - How to observe PagedList LiveData as it submit result asynchronously? 如何在房间数据库类中获取或观察实时数据 - how to get or observe livedata in room database class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM