![](/img/trans.png)
[英]Launching a coroutine from a Fragment using viewLifecycleOwner's lifecycleScope
[英]What's the point of using LiveData in ViewModel if I can use Flow and StateFlow with lifecycleScope \ viewLifecycleOwner.lifecycleScope
Flow
有很多算子, LiveData
只有 3 個(Transformations)。
除了 StateFlow 仍處於試驗
階段之外,還有什么理由繼續使用 LiveData 嗎?
UPD。 在 kotlinx.coroutines 1.4.0 中,StateFlow、SharedFlow 和相應的算子被提升為穩定的 API
現在沒有太多理由使用 LiveData。 (State)Flow/Coroutines 還通過lifecycleScope.launchWhenCreated/Started/Resumed 帶來了新的可能性,LiveData 很難做到。
但是需要 LiveData 有一個原因 - DataBinding。 它目前不支持觀察 Flow。
編輯:Android Studio 4.3 中的 DataBinding 將支持 StateFlow: https://twitter.com/manuelvicnt/status/1314621067831521282
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.