简体   繁体   English

如果我可以将 Flow 和 StateFlow 与生命周期范围 \ viewLifecycleOwner.lifecycleScope 一起使用,那么在 ViewModel 中使用 LiveData 有什么意义

[英]What's the point of using LiveData in ViewModel if I can use Flow and StateFlow with lifecycleScope \ viewLifecycleOwner.lifecycleScope

Flow has a lot of operators, LiveData has only 3 (Transformations). Flow有很多算子, LiveData只有 3 个(Transformations)。 Is there any reason to keep using LiveData except StateFlow is still experimental ? 除了 StateFlow 仍处于试验 阶段之外,还有什么理由继续使用 LiveData 吗?

UPD. UPD。 StateFlow, SharedFlow and corresponding operators are promoted to stable API in kotlinx.coroutines 1.4.0在 kotlinx.coroutines 1.4.0 中,StateFlow、SharedFlow 和相应的算子被提升为稳定的 API

There is not much reason to use LiveData nowadays.现在没有太多理由使用 LiveData。 (State)Flow/Coroutines also brings new possibilites via lifecycleScope.launchWhenCreated/Started/Resumed, hard to do with LiveData. (State)Flow/Coroutines 还通过lifecycleScope.launchWhenCreated/Started/Resumed 带来了新的可能性,LiveData 很难做到。

But there is one reason when LiveData is needed - DataBinding.但是需要 LiveData 有一个原因 - DataBinding。 It currently doesn't support observing Flow.它目前不支持观察 Flow。

EDIT: there is going to be support for StateFlow in DataBinding in Android Studio 4.3: https://twitter.com/manuelvicnt/status/1314621067831521282编辑:Android Studio 4.3 中的 DataBinding 将支持 StateFlow: https://twitter.com/manuelvicnt/status/1314621067831521282

暂无
暂无

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

相关问题 使用 viewLifecycleOwner 的 lifecycleScope 从 Fragment 启动协程 - Launching a coroutine from a Fragment using viewLifecycleOwner's lifecycleScope 如何在 WebSocketListener 中使用暂停 function 或 viewModelScope/lifecycleScope? - How can I use suspend function or viewModelScope/lifecycleScope in WebSocketListener? 使用 lifeCycleScope 后出现 IllegalStateException - IllegalStateException after using lifeCycleScope 使用 MainScope() 和 requireActivity().lifecycleScope 在片段内启动协程有什么区别? - What's the difference between launching a coroutine inside a fragment using `MainScope()` and `requireActivity().lifecycleScope`? lifecycleScope.launchWhenStarted 是否安全? 如果不安全,在什么情况下? - Is lifecycleScope.launchWhenStarted safe or not ? If it is not safe, for what case? 为什么片段的上下文在lifecycleScope中为空 - Why the fragment's context null inside lifecycleScope 为什么Android lifecycleScope可以自动取消 - Why Android lifecycleScope can be automatically cancelled 如何将WearableActivity与LiveData和ViewModel结合使用 - How can I use WearableActivity with LiveData and ViewModel 是否可以使用 Dagger 将当前 Activity 的 lifecycleScope 注入到非 Activity class 中? - Can the lifecycleScope for the current Activity be injected into a non-Activity class using Dagger? 如何在 Jetpack Compose 的 viewModel 内将 DataStore 首选项的 Flow 转换为 StateFlow - How can I convert DataStore preferences' Flow to StateFlow inside a viewModel in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM