{"id":486,"date":"2025-05-07T11:12:59","date_gmt":"2025-05-07T03:12:59","guid":{"rendered":"https:\/\/trantor.ink\/?p=486"},"modified":"2025-05-07T11:12:59","modified_gmt":"2025-05-07T03:12:59","slug":"%e5%9f%ba%e4%ba%8e-kotlin-%e5%8d%8f%e7%a8%8b%e7%9a%84-livedata-%e6%9b%bf%e4%bb%a3%e6%96%b9%e6%a1%88%ef%bc%8c%e7%89%b9%e6%80%a7%e8%af%a6%e8%a7%a3%e5%8f%8a%e5%af%b9%e6%af%94-2","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=486","title":{"rendered":"\u57fa\u4e8e Kotlin \u534f\u7a0b\u7684 LiveData \u66ff\u4ee3\u65b9\u6848\uff0c\u7279\u6027\u8be6\u89e3\u53ca\u5bf9\u6bd4"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=486\/#LiveData_%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88%EF%BC%9AStateFlow_%E5%92%8C_SharedFlow\" >LiveData \u66ff\u4ee3\u65b9\u6848\uff1aStateFlow \u548c SharedFlow<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=486\/#1_StateFlow\" >1. StateFlow<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/trantor.ink\/?p=486\/#2_SharedFlow\" >2. SharedFlow<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/trantor.ink\/?p=486\/#LiveData_vs_StateFlow_vs_SharedFlow_%E5%AF%B9%E6%AF%94\" >LiveData vs. StateFlow vs. SharedFlow \u5bf9\u6bd4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/trantor.ink\/?p=486\/#%E6%80%BB%E7%BB%93%E4%B8%8E%E9%80%89%E6%8B%A9%E5%BB%BA%E8%AE%AE\" >\u603b\u7ed3\u4e0e\u9009\u62e9\u5efa\u8bae<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>\u597d\u7684\uff0c\u6211\u4eec\u6765\u63a2\u8ba8\u4e00\u4e0b\u57fa\u4e8e Kotlin \u534f\u7a0b\u7684 LiveData \u66ff\u4ee3\u65b9\u6848\uff0c\u8be6\u7ec6\u4e86\u89e3\u5b83\u4eec\u7684\u7279\u6027\uff0c\u5e76\u4e0e LiveData \u8fdb\u884c\u5bf9\u6bd4\u3002<\/p>\n\n\n\n<p>\u5728 Android \u5f00\u53d1\u4e2d\uff0cLiveData \u4e00\u76f4\u662f\u54cd\u5e94\u5f0f UI \u7f16\u7a0b\u7684\u5e38\u7528\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u611f\u77e5\u751f\u547d\u5468\u671f\uff0c\u5e76\u4e14\u53ef\u4ee5\u5f88\u597d\u5730\u4e0e ViewModel \u914d\u5408\u4f7f\u7528\u3002\u7136\u800c\uff0c\u968f\u7740 Kotlin \u534f\u7a0b\u7684\u666e\u53ca\uff0c\u51fa\u73b0\u4e86\u529f\u80fd\u66f4\u5f3a\u5927\u3001\u66f4\u7075\u6d3b\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u7279\u522b\u662f <code>StateFlow<\/code> \u548c <code>SharedFlow<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LiveData_%E6%9B%BF%E4%BB%A3%E6%96%B9%E6%A1%88%EF%BC%9AStateFlow_%E5%92%8C_SharedFlow\"><\/span>LiveData \u66ff\u4ee3\u65b9\u6848\uff1a<code>StateFlow<\/code> \u548c <code>SharedFlow<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>StateFlow<\/code> \u548c <code>SharedFlow<\/code> \u662f Kotlin \u534f\u7a0b\u5e93\u4e2d <code>Flow<\/code> API \u7684\u4e00\u90e8\u5206\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e0e LiveData \u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5177\u6709\u534f\u7a0b\u7684\u5f3a\u5927\u4f18\u52bf\u548c\u7075\u6d3b\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_StateFlow\"><\/span>1. <code>StateFlow<\/code><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><code>StateFlow<\/code> \u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684 <code>SharedFlow<\/code>\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u53ef\u89c2\u5bdf\u7684\u72b6\u6001\u5bb9\u5668\u3002\u5b83\u603b\u662f\u6709\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u5e76\u4e14\u53ea\u53d1\u51fa\u6700\u65b0\u7684\u72b6\u6001\u66f4\u65b0\u3002\u5982\u679c\u4f60\u719f\u6089 RxJava\uff0c<code>StateFlow<\/code> \u7c7b\u4f3c\u4e8e <code>BehaviorSubject<\/code> \u6216 LiveData \u672c\u8eab\u3002<\/p>\n\n\n\n<p><strong>\u7279\u6027\u8be6\u89e3\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u72b6\u6001\u5bb9\u5668 (State Holder):<\/strong> <code>StateFlow<\/code> \u88ab\u8bbe\u8ba1\u7528\u6765\u6301\u6709\u548c\u66b4\u9732\u5f53\u524d\u72b6\u6001\u3002\u5b83\u603b\u662f\u6709\u4e00\u4e2a\u503c\u3002\u5f53\u4f60\u8ba2\u9605\u5b83\u65f6\uff0c\u4f60\u4f1a\u7acb\u5373\u6536\u5230\u6700\u65b0\u7684\u503c\u3002<\/li>\n\n\n\n<li><strong>\u70ed\u6570\u636e\u6d41 (Hot Flow):<\/strong> <code>StateFlow<\/code> \u662f\u4e00\u4e2a\u70ed\u6570\u636e\u6d41\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u5728\u6ca1\u6709\u6536\u96c6\u5668\uff08subscriber\uff09\u7684\u60c5\u51b5\u4e0b\u4e5f\u4f1a\u4fdd\u6301\u6d3b\u52a8\u72b6\u6001\u5e76\u6301\u6709\u6700\u65b0\u7684\u503c\u3002\u5f53\u65b0\u7684\u6536\u96c6\u5668\u5f00\u59cb\u6536\u96c6\u65f6\uff0c\u5b83\u4f1a\u7acb\u5373\u6536\u5230\u5f53\u524d\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u5355\u4e00\u6700\u65b0\u503c (Conflation):<\/strong> \u5982\u679c\u6709\u591a\u4e2a\u503c\u5feb\u901f\u53d1\u51fa\uff0c\u6536\u96c6\u5668\u53ea\u4f1a\u63a5\u6536\u5230\u6700\u65b0\u7684\u503c\uff0c\u4e2d\u95f4\u7684\u503c\u4f1a\u88ab\u201c\u5408\u5e76\u201d\u6216\u201c\u5ffd\u7565\u201d\u3002\u8fd9\u5bf9\u4e8e\u8868\u793a UI \u72b6\u6001\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u4f60\u901a\u5e38\u53ea\u5173\u5fc3\u6700\u65b0\u7684\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u53d8\u7248\u672c (<code>MutableStateFlow<\/code>):<\/strong> \u7c7b\u4f3c\u4e8e <code>LiveData<\/code> \u548c <code>MutableLiveData<\/code>\uff0c<code>StateFlow<\/code> \u4e5f\u6709\u4e00\u4e2a\u53ef\u53d8\u7248\u672c <code>MutableStateFlow<\/code>\uff0c\u5141\u8bb8\u4f60\u901a\u8fc7 <code>value<\/code> \u5c5e\u6027\u6765\u66f4\u65b0\u5176\u6301\u6709\u7684\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f\u611f\u77e5 (Lifecycle-Aware Collection &#8211; \u901a\u8fc7 <code>repeatOnLifecycle<\/code> \u6216 <code>flowWithLifecycle<\/code>):<\/strong> \u867d\u7136 <code>StateFlow<\/code> \u672c\u8eab\u4e0d\u662f Android \u751f\u547d\u5468\u671f\u611f\u77e5\u7684\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>LifecycleOwner.lifecycleScope.launchWhenStarted<\/code>\u3001<code>LifecycleOwner.lifecycleScope.launchWhenResumed<\/code> \u6216\u66f4\u63a8\u8350\u7684 <code>LifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { ... } }<\/code> \u6216 <code>flow.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED).collect { ... }<\/code> \u6765\u5b89\u5168\u5730\u4ece UI \u5c42\u6536\u96c6 Flow\uff0c\u4f7f\u5176\u80fd\u591f\u611f\u77e5\u751f\u547d\u5468\u671f\u3002<\/li>\n\n\n\n<li><strong>\u80cc\u538b\u5904\u7406 (Backpressure):<\/strong> <code>StateFlow<\/code> \u7684\u8bbe\u8ba1\u5929\u7136\u5730\u5904\u7406\u4e86\u80cc\u538b\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u53ea\u5173\u5fc3\u6700\u65b0\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6d4b\u8bd5\u6027 (Testability):<\/strong> \u7531\u4e8e\u662f Kotlin \u534f\u7a0b\u7684\u4e00\u90e8\u5206\uff0c\u6d4b\u8bd5 <code>StateFlow<\/code> \u76f8\u5bf9\u76f4\u63a5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>kotlinx-coroutines-test<\/code> \u5e93\u63d0\u4f9b\u7684\u5de5\u5177\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u5b89\u5168 (Thread-Safety):<\/strong> <code>MutableStateFlow<\/code> \u7684 <code>value<\/code> \u5c5e\u6027\u7684\u66f4\u65b0\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5982\u4f55\u4f7f\u7528 <code>MutableStateFlow<\/code> (\u5728 ViewModel \u4e2d):<\/strong><\/p>\n\n\n\n<p>Kotlin<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport androidx.lifecycle.ViewModel\n\nclass MyViewModel : ViewModel() {\n\n    \/\/ \u79c1\u6709\u7684\u53ef\u53d8 StateFlow\uff0c\u7528\u4e8e\u5728 ViewModel \u5185\u90e8\u66f4\u65b0\u6570\u636e\n    private val _uiState = MutableStateFlow&lt;MyUiState>(MyUiState.Loading)\n    \/\/ \u5bf9\u5916\u66b4\u9732\u7684\u4e0d\u53ef\u53d8 StateFlow\uff0c\u4f9b UI \u5c42\u89c2\u5bdf\n    val uiState: StateFlow&lt;MyUiState> = _uiState\n\n    fun fetchData() {\n        _uiState.value = MyUiState.Loading\n        \/\/ \u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\n        viewModelScope.launch {\n            delay(2000) \/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n            _uiState.value = MyUiState.Success(\"Data loaded successfully!\")\n        }\n    }\n\n    fun setError(message: String) {\n        _uiState.value = MyUiState.Error(message)\n    }\n}\n\n\/\/ \u5b9a\u4e49 UI \u72b6\u6001\u7684\u5bc6\u5c01\u7c7b\nsealed class MyUiState {\n    object Loading : MyUiState()\n    data class Success(val data: String) : MyUiState()\n    data class Error(val message: String) : MyUiState()\n}\n<\/pre>\n\n\n\n<p><strong>\u5982\u4f55\u5728 UI \u5c42 (Activity\/Fragment) \u6536\u96c6 <code>StateFlow<\/code>:<\/strong><\/p>\n\n\n\n<p>Kotlin<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import androidx.appcompat.app.AppCompatActivity\nimport android.os.Bundle\nimport androidx.lifecycle.ViewModelProvider\nimport androidx.lifecycle.lifecycleScope\nimport androidx.lifecycle.repeatOnLifecycle\nimport kotlinx.coroutines.launch\nimport com.example.app.R \/\/ \u5047\u8bbe\u4f60\u7684 R \u6587\u4ef6\u8def\u5f84\n\nclass MyActivity : AppCompatActivity() {\n\n    private lateinit var viewModel: MyViewModel\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_my) \/\/ \u5047\u8bbe\u4f60\u7684\u5e03\u5c40\u6587\u4ef6\n\n        viewModel = ViewModelProvider(this).get(MyViewModel::class.java)\n\n        \/\/ \u4f7f\u7528 repeatOnLifecycle \u6765\u5b89\u5168\u5730\u6536\u96c6 Flow\n        lifecycleScope.launch {\n            repeatOnLifecycle(Lifecycle.State.STARTED) {\n                viewModel.uiState.collect { uiState ->\n                    \/\/ \u66f4\u65b0 UI\n                    when (uiState) {\n                        is MyUiState.Loading -> {\n                            \/\/ \u663e\u793a\u52a0\u8f7d\u6307\u793a\u5668\n                            println(\"UI State: Loading\")\n                        }\n                        is MyUiState.Success -> {\n                            \/\/ \u663e\u793a\u6210\u529f\u6570\u636e\n                            println(\"UI State: Success - ${uiState.data}\")\n                        }\n                        is MyUiState.Error -> {\n                            \/\/ \u663e\u793a\u9519\u8bef\u4fe1\u606f\n                            println(\"UI State: Error - ${uiState.message}\")\n                        }\n                    }\n                }\n            }\n        }\n\n        \/\/ \u89e6\u53d1\u6570\u636e\u52a0\u8f7d\n        viewModel.fetchData()\n    }\n}\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_SharedFlow\"><\/span>2. <code>SharedFlow<\/code><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p><code>SharedFlow<\/code> \u662f\u4e00\u79cd\u66f4\u901a\u7528\u7684\u70ed\u6570\u636e\u6d41\uff0c\u5b83\u53ef\u4ee5\u5c06\u53d1\u51fa\u7684\u503c\u5e7f\u64ad\u7ed9\u591a\u4e2a\u6536\u96c6\u5668\u3002\u4e0e <code>StateFlow<\/code> \u4e0d\u540c\uff0c<code>SharedFlow<\/code> \u4e0d\u4e00\u5b9a\u6709\u521d\u59cb\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u914d\u7f6e\u5176\u91cd\u64ad\u7f13\u5b58\uff08replay cache\uff09\u548c\u7f13\u51b2\u7b56\u7565\u3002<\/p>\n\n\n\n<p><strong>\u7279\u6027\u8be6\u89e3\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u5e7f\u64ad (Event Broadcasting):<\/strong> <code>SharedFlow<\/code> \u975e\u5e38\u9002\u5408\u7528\u4e8e\u5e7f\u64ad\u4e00\u6b21\u6027\u4e8b\u4ef6\uff0c\u4f8b\u5982\u5bfc\u822a\u4e8b\u4ef6\u3001\u663e\u793a Snackbar \u6d88\u606f\u7b49\u3002\u5b83\u53ef\u4ee5\u6709\u591a\u4e2a\u6536\u96c6\u5668\uff0c\u5e76\u4e14\u6bcf\u4e2a\u6536\u96c6\u5668\u90fd\u4f1a\u6536\u5230\u53d1\u51fa\u7684\u4e8b\u4ef6\uff08\u53d6\u51b3\u4e8e\u5176\u914d\u7f6e\uff09\u3002<\/li>\n\n\n\n<li><strong>\u70ed\u6570\u636e\u6d41 (Hot Flow):<\/strong> \u4e0e <code>StateFlow<\/code> \u7c7b\u4f3c\uff0c<code>SharedFlow<\/code> \u4e5f\u662f\u70ed\u6570\u636e\u6d41\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u914d\u7f6e\u7684\u91cd\u64ad\u7f13\u5b58 (<code>replay<\/code>):<\/strong> \u4f60\u53ef\u4ee5\u914d\u7f6e <code>SharedFlow<\/code> \u7f13\u5b58\u591a\u5c11\u4e2a\u6700\u8fd1\u53d1\u51fa\u7684\u503c\uff0c\u5e76\u5728\u65b0\u7684\u6536\u96c6\u5668\u8ba2\u9605\u65f6\u5c06\u8fd9\u4e9b\u7f13\u5b58\u7684\u503c\u53d1\u9001\u7ed9\u5b83\u4eec\u3002\u5982\u679c <code>replay = 0<\/code>\uff08\u9ed8\u8ba4\uff09\uff0c\u65b0\u7684\u6536\u96c6\u5668\u4e0d\u4f1a\u6536\u5230\u4efb\u4f55\u5386\u53f2\u503c\uff0c\u53ea\u4f1a\u6536\u5230\u8ba2\u9605\u4e4b\u540e\u53d1\u51fa\u7684\u65b0\u503c\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u914d\u7f6e\u7684\u7f13\u51b2\u7b56\u7565 (<code>extraBufferCapacity<\/code>, <code>onBufferOverflow<\/code>):<\/strong> \u53ef\u4ee5\u914d\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\u4ee5\u53ca\u5f53\u7f13\u51b2\u533a\u6ee1\u65f6\u7684\u884c\u4e3a\uff08\u4f8b\u5982\uff0c\u6302\u8d77\u53d1\u9001\u8005\u3001\u4e22\u5f03\u6700\u65b0\u503c\u3001\u4e22\u5f03\u6700\u65e7\u503c\uff09\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u53d8\u7248\u672c (<code>MutableSharedFlow<\/code>):<\/strong> \u5141\u8bb8\u901a\u8fc7 <code>emit()<\/code> \u51fd\u6570\u53d1\u9001\u503c\uff0c\u6216\u901a\u8fc7 <code>tryEmit()<\/code> \u5c1d\u8bd5\u53d1\u9001\u503c\uff08\u5982\u679c\u7f13\u51b2\u533a\u672a\u6ee1\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6ca1\u6709\u5185\u7f6e\u7684\u201c\u5f53\u524d\u72b6\u6001\u201d\u6982\u5ff5:<\/strong> \u4e0e <code>StateFlow<\/code> \u4e0d\u540c\uff0c<code>SharedFlow<\/code> \u672c\u8eab\u4e0d\u7ef4\u62a4\u4e00\u4e2a\u201c\u5f53\u524d\u72b6\u6001\u201d\u3002\u5b83\u66f4\u4fa7\u91cd\u4e8e\u4e8b\u4ef6\u6d41\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f\u611f\u77e5 (Lifecycle-Aware Collection):<\/strong> \u4e0e <code>StateFlow<\/code> \u4e00\u6837\uff0c\u9700\u8981\u914d\u5408 <code>repeatOnLifecycle<\/code> \u6216 <code>flowWithLifecycle<\/code> \u6765\u5b9e\u73b0\u751f\u547d\u5468\u671f\u5b89\u5168\u7684\u6536\u96c6\u3002<\/li>\n\n\n\n<li><strong>\u80cc\u538b\u5904\u7406 (Backpressure):<\/strong> \u53ef\u4ee5\u901a\u8fc7\u7f13\u51b2\u7b56\u7565\u8fdb\u884c\u914d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6d4b\u8bd5\u6027 (Testability):<\/strong> \u540c\u6837\u53ef\u4ee5\u4f7f\u7528 <code>kotlinx-coroutines-test<\/code> \u8fdb\u884c\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5982\u4f55\u4f7f\u7528 <code>MutableSharedFlow<\/code> (\u5728 ViewModel \u4e2d\uff0c\u7528\u4e8e\u4e00\u6b21\u6027\u4e8b\u4ef6):<\/strong><\/p>\n\n\n\n<p>Kotlin<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import kotlinx.coroutines.channels.BufferOverflow\nimport kotlinx.coroutines.flow.MutableSharedFlow\nimport kotlinx.coroutines.flow.SharedFlow\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\nimport kotlinx.coroutines.launch\n\nclass EventViewModel : ViewModel() {\n\n    \/\/ \u79c1\u6709\u7684\u53ef\u53d8 SharedFlow\n    \/\/ replay = 0: \u65b0\u7684\u8ba2\u9605\u8005\u4e0d\u4f1a\u6536\u5230\u65e7\u4e8b\u4ef6\n    \/\/ extraBufferCapacity = 1: \u5141\u8bb8\u7f13\u51b2\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u4ee5\u9632\u53d1\u5c04\u65f6\u6ca1\u6709\u8ba2\u9605\u8005\n    \/\/ onBufferOverflow = BufferOverflow.DROP_OLDEST: \u5982\u679c\u7f13\u51b2\u533a\u6ee1\u4e86\uff0c\u4e22\u5f03\u6700\u65e7\u7684\u4e8b\u4ef6\n    private val _events = MutableSharedFlow&lt;Event>(\n        replay = 0,\n        extraBufferCapacity = 1,\n        onBufferOverflow = BufferOverflow.DROP_OLDEST\n    )\n    \/\/ \u5bf9\u5916\u66b4\u9732\u7684\u4e0d\u53ef\u53d8 SharedFlow\n    val events: SharedFlow&lt;Event> = _events\n\n    fun triggerShowSnackbarEvent(message: String) {\n        viewModelScope.launch {\n            _events.emit(Event.ShowSnackbar(message))\n        }\n    }\n\n    fun triggerNavigationEvent(destination: String) {\n        \/\/ tryEmit \u662f\u975e\u6302\u8d77\u51fd\u6570\uff0c\u5982\u679c\u7f13\u51b2\u533a\u5df2\u6ee1\u4e14\u914d\u7f6e\u4e3a\u6302\u8d77\uff0c\u5219\u4f1a\u5931\u8d25\n        \/\/ \u5bf9\u4e8e\u4e0d\u60f3\u6302\u8d77\u53d1\u9001\u8005\u7684\u60c5\u51b5\u53ef\u4ee5\u4f7f\u7528\n        val success = _events.tryEmit(Event.NavigateTo(destination))\n        if (!success) {\n            \/\/ \u5904\u7406\u53d1\u9001\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u8bb0\u5f55\u65e5\u5fd7\u6216\u7a0d\u540e\u91cd\u8bd5\n            println(\"Failed to emit navigation event: Buffer might be full.\")\n        }\n    }\n}\n\n\/\/ \u5b9a\u4e49\u4e8b\u4ef6\u7684\u5bc6\u5c01\u7c7b\nsealed class Event {\n    data class ShowSnackbar(val message: String) : Event()\n    data class NavigateTo(val destination: String) : Event()\n}\n<\/pre>\n\n\n\n<p><strong>\u5982\u4f55\u5728 UI \u5c42 (Activity\/Fragment) \u6536\u96c6 <code>SharedFlow<\/code>:<\/strong><\/p>\n\n\n\n<p>Kotlin<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import androidx.appcompat.app.AppCompatActivity\nimport android.os.Bundle\nimport android.widget.Toast\nimport androidx.lifecycle.ViewModelProvider\nimport androidx.lifecycle.lifecycleScope\nimport androidx.lifecycle.repeatOnLifecycle\nimport kotlinx.coroutines.launch\nimport com.example.app.R \/\/ \u5047\u8bbe\u4f60\u7684 R \u6587\u4ef6\u8def\u5f84\n\nclass EventActivity : AppCompatActivity() {\n\n    private lateinit var viewModel: EventViewModel\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_event) \/\/ \u5047\u8bbe\u4f60\u7684\u5e03\u5c40\u6587\u4ef6\n\n        viewModel = ViewModelProvider(this).get(EventViewModel::class.java)\n\n        lifecycleScope.launch {\n            repeatOnLifecycle(Lifecycle.State.STARTED) {\n                viewModel.events.collect { event ->\n                    when (event) {\n                        is Event.ShowSnackbar -> {\n                            Toast.makeText(this@EventActivity, event.message, Toast.LENGTH_SHORT).show()\n                            println(\"Event: Show Snackbar - ${event.message}\")\n                        }\n                        is Event.NavigateTo -> {\n                            \/\/ \u5904\u7406\u5bfc\u822a\u903b\u8f91\n                            println(\"Event: Navigate To - ${event.destination}\")\n                        }\n                    }\n                }\n            }\n        }\n\n        \/\/ \u89e6\u53d1\u4e8b\u4ef6\n        viewModel.triggerShowSnackbarEvent(\"Hello from SharedFlow!\")\n        \/\/ \u53ef\u4ee5\u5728\u6309\u94ae\u70b9\u51fb\u7b49\u64cd\u4f5c\u4e2d\u89e6\u53d1\n        \/\/ button.setOnClickListener {\n        \/\/     viewModel.triggerNavigationEvent(\"ProfileScreen\")\n        \/\/ }\n    }\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LiveData_vs_StateFlow_vs_SharedFlow_%E5%AF%B9%E6%AF%94\"><\/span><code>LiveData<\/code> vs. <code>StateFlow<\/code> vs. <code>SharedFlow<\/code> \u5bf9\u6bd4<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>\u7279\u6027<\/strong><\/td><td><strong>LiveData<\/strong><\/td><td><strong>StateFlow<\/strong><\/td><td><strong>SharedFlow<\/strong><\/td><\/tr><tr><td><strong>\u4e3b\u8981\u7528\u9014<\/strong><\/td><td>UI \u72b6\u6001\u6301\u6709\uff0c\u6570\u636e\u89c2\u5bdf<\/td><td>UI \u72b6\u6001\u6301\u6709\uff0c\u5355\u4e00\u6700\u65b0\u72b6\u6001\u5e7f\u64ad<\/td><td>\u4e8b\u4ef6\u5e7f\u64ad\uff0c\u591a\u4e2a\u6536\u96c6\u5668\u5171\u4eab\u6570\u636e\u6d41<\/td><\/tr><tr><td><strong>\u521d\u59cb\u503c<\/strong><\/td><td>\u53ef\u4ee5\u6ca1\u6709\uff08\u4f46\u901a\u5e38\u6709\uff09<\/td><td><strong>\u5fc5\u987b\u6709<\/strong><\/td><td>\u53ef\u4ee5\u6ca1\u6709<\/td><\/tr><tr><td><strong>\u503c\u7c7b\u578b<\/strong><\/td><td>\u4efb\u4f55\u7c7b\u578b<\/td><td>\u4efb\u4f55\u7c7b\u578b<\/td><td>\u4efb\u4f55\u7c7b\u578b<\/td><\/tr><tr><td><strong>\u611f\u77e5\u751f\u547d\u5468\u671f<\/strong><\/td><td><strong>\u5185\u7f6e<\/strong> (\u81ea\u52a8\u5904\u7406\u8ba2\u9605\u548c\u53d6\u6d88\u8ba2\u9605)<\/td><td><strong>\u4e0d\u5185\u7f6e<\/strong> (\u9700\u914d\u5408 <code>lifecycleScope.repeatOnLifecycle<\/code> \u6216 <code>flowWithLifecycle<\/code>)<\/td><td><strong>\u4e0d\u5185\u7f6e<\/strong> (\u9700\u914d\u5408 <code>lifecycleScope.repeatOnLifecycle<\/code> \u6216 <code>flowWithLifecycle<\/code>)<\/td><\/tr><tr><td><strong>\u6570\u636e\u6d41\u7c7b\u578b<\/strong><\/td><td>\u70ed\u6570\u636e\u6d41<\/td><td>\u70ed\u6570\u636e\u6d41<\/td><td>\u70ed\u6570\u636e\u6d41<\/td><\/tr><tr><td><strong>\u503c\u5408\u5e76 (Conflation)<\/strong><\/td><td>\u53ea\u53d1\u9001\u6700\u65b0\u503c (\u5982\u679c Activity\/Fragment \u4e0d\u6d3b\u8dc3\u5219\u4e0d\u53d1\u9001\uff0c\u6d3b\u8dc3\u540e\u53d1\u9001\u6700\u65b0\u503c)<\/td><td>\u53ea\u53d1\u9001\u6700\u65b0\u503c (\u6536\u96c6\u5668\u603b\u662f\u83b7\u5f97\u6700\u65b0\u72b6\u6001)<\/td><td>\u53ef\u914d\u7f6e (\u901a\u8fc7 <code>replay<\/code> \u548c\u7f13\u51b2\u7b56\u7565\uff0c\u9ed8\u8ba4\u4e0d\u91cd\u64ad\u65e7\u503c)<\/td><\/tr><tr><td><strong>\u53ef\u53d8\u6027<\/strong><\/td><td><code>MutableLiveData<\/code><\/td><td><code>MutableStateFlow<\/code><\/td><td><code>MutableSharedFlow<\/code><\/td><\/tr><tr><td><strong>\u534f\u7a0b\u96c6\u6210<\/strong><\/td><td>\u6709\u9650 (\u901a\u8fc7 <code>liveData<\/code> \u6784\u5efa\u5668)<\/td><td><strong>\u539f\u751f\u96c6\u6210<\/strong> (Kotlin \u534f\u7a0b\u7684\u4e00\u90e8\u5206)<\/td><td><strong>\u539f\u751f\u96c6\u6210<\/strong> (Kotlin \u534f\u7a0b\u7684\u4e00\u90e8\u5206)<\/td><\/tr><tr><td><strong>\u80cc\u538b\u5904\u7406<\/strong><\/td><td>\u81ea\u52a8\u5904\u7406 (\u56e0\u4e3a\u751f\u547d\u5468\u671f\u611f\u77e5\u548c\u503c\u5408\u5e76)<\/td><td>\u5929\u7136\u5904\u7406 (\u56e0\u53ea\u5173\u5fc3\u6700\u65b0\u72b6\u6001)<\/td><td>\u53ef\u901a\u8fc7\u7f13\u51b2\u7b56\u7565\u914d\u7f6e<\/td><\/tr><tr><td><strong>\u7ebf\u7a0b<\/strong><\/td><td>\u4e3b\u7ebf\u7a0b\u66f4\u65b0 (\u901a\u8fc7 <code>setValue<\/code>)\uff0c\u540e\u53f0\u7ebf\u7a0b\u66f4\u65b0 (\u901a\u8fc7 <code>postValue<\/code>)<\/td><td><code>value<\/code> \u66f4\u65b0\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6536\u96c6\u901a\u5e38\u5728\u6307\u5b9a <code>CoroutineContext<\/code> \u8fdb\u884c<\/td><td><code>emit<\/code> \u662f\u6302\u8d77\u51fd\u6570\uff0c<code>tryEmit<\/code> \u662f\u975e\u6302\u8d77\u51fd\u6570\uff1b\u6536\u96c6\u5728\u6307\u5b9a <code>CoroutineContext<\/code> \u8fdb\u884c<\/td><\/tr><tr><td><strong>\u64cd\u4f5c\u7b26<\/strong><\/td><td>\u6709\u9650\u7684\u8f6c\u6362\u64cd\u4f5c (<code>Transformations.map<\/code>, <code>switchMap<\/code>)<\/td><td>\u4e30\u5bcc\u7684 Flow \u64cd\u4f5c\u7b26 (<code>map<\/code>, <code>filter<\/code>, <code>combine<\/code> \u7b49)<\/td><td>\u4e30\u5bcc\u7684 Flow \u64cd\u4f5c\u7b26 (<code>map<\/code>, <code>filter<\/code>, <code>shareIn<\/code> \u7b49)<\/td><\/tr><tr><td><strong>\u6d4b\u8bd5<\/strong><\/td><td>\u9700\u8981\u7279\u5b9a\u89c4\u5219 (\u5982 <code>InstantTaskExecutorRule<\/code>)<\/td><td>\u4f7f\u7528 <code>kotlinx-coroutines-test<\/code><\/td><td>\u4f7f\u7528 <code>kotlinx-coroutines-test<\/code><\/td><\/tr><tr><td><strong>\u9002\u7528\u573a\u666f<\/strong><\/td><td>\u7b80\u5355\u7684 UI \u72b6\u6001\u7ba1\u7406<\/td><td>\u590d\u6742\u7684 UI \u72b6\u6001\u7ba1\u7406\uff0c\u9700\u8981\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\u548c Flow \u64cd\u4f5c\u7b26<\/td><td>\u4e00\u6b21\u6027\u4e8b\u4ef6 (Snackbar, \u5bfc\u822a)\uff0c\u9700\u8981\u5e7f\u64ad\u7ed9\u591a\u4e2a\u8ba2\u9605\u8005\u7684\u573a\u666f\uff0c\u9700\u8981\u7f13\u5b58\u548c\u91cd\u64ad\u7684\u573a\u666f<\/td><\/tr><tr><td><strong>Java \u4e92\u64cd\u4f5c\u6027<\/strong><\/td><td>\u826f\u597d<\/td><td>\u826f\u597d (\u4f46\u66f4\u504f\u5411 Kotlin)<\/td><td>\u826f\u597d (\u4f46\u66f4\u504f\u5411 Kotlin)<\/td><\/tr><tr><td><strong>\u7a7a\u5b89\u5168<\/strong><\/td><td>\u9700\u8981\u6ce8\u610f\u53ef\u7a7a\u6027<\/td><td>Kotlin \u539f\u751f\u652f\u6301\u7a7a\u5b89\u5168<\/td><td>Kotlin \u539f\u751f\u652f\u6301\u7a7a\u5b89\u5168<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%BB%E7%BB%93%E4%B8%8E%E9%80%89%E6%8B%A9%E5%BB%BA%E8%AE%AE\"><\/span>\u603b\u7ed3\u4e0e\u9009\u62e9\u5efa\u8bae<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5982\u679c\u4f60\u4e3b\u8981\u5728 Java \u9879\u76ee\u4e2d\uff0c\u6216\u8005\u9879\u76ee\u975e\u5e38\u7b80\u5355\uff0c\u4e14\u5bf9 LiveData \u5df2\u7ecf\u975e\u5e38\u719f\u6089\uff0cLiveData \u4ecd\u7136\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/strong> \u5b83\u7684\u7b80\u5355\u6027\u548c\u5185\u7f6e\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u662f\u5176\u4e3b\u8981\u4f18\u70b9\u3002<\/li>\n\n\n\n<li><strong>\u5bf9\u4e8e\u65b0\u7684 Kotlin \u9879\u76ee\uff0c\u6216\u8005\u5f53\u4f60\u9700\u8981\u66f4\u9ad8\u7ea7\u7684\u54cd\u5e94\u5f0f\u7f16\u7a0b\u80fd\u529b\u65f6\uff0c<code>StateFlow<\/code> \u662f LiveData \u7684\u4e00\u4e2a\u4f18\u79c0\u66ff\u4ee3\u54c1\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u7ba1\u7406 UI \u72b6\u6001\u3002<\/strong>\n<ul class=\"wp-block-list\">\n<li>\u5b83\u63d0\u4f9b\u4e86\u4e0e LiveData \u7c7b\u4f3c\u7684\u72b6\u6001\u6301\u6709\u80fd\u529b\uff0c\u4f46\u4e0e\u534f\u7a0b\u548c Flow API \u7ed3\u5408\u5f97\u66f4\u7d27\u5bc6\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u64cd\u4f5c\u7b26\u548c\u66f4\u597d\u7684\u6027\u80fd\u3002<\/li>\n\n\n\n<li>\u867d\u7136\u9700\u8981\u624b\u52a8\u5904\u7406\u751f\u547d\u5468\u671f\u611f\u77e5\u6536\u96c6\uff0c\u4f46 <code>repeatOnLifecycle<\/code> API \u4f7f\u5f97\u8fd9\u4e00\u70b9\u53d8\u5f97\u76f8\u5bf9\u7b80\u5355\u548c\u5b89\u5168\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5f53\u4f60\u9700\u8981\u5904\u7406\u4e00\u6b21\u6027\u4e8b\u4ef6\uff08\u4f8b\u5982\u5bfc\u822a\u3001\u663e\u793a Toast\/Snackbar\uff09\u6216\u8005\u9700\u8981\u5c06\u6570\u636e\u6d41\u5e7f\u64ad\u7ed9\u591a\u4e2a\u53ef\u80fd\u7684\u8ba2\u9605\u8005\u65f6\uff0c<code>SharedFlow<\/code> \u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>StateFlow<\/code> \u4e0d\u9002\u5408\u7528\u4e8e\u4e8b\u4ef6\uff0c\u56e0\u4e3a\u4e8b\u4ef6\u901a\u5e38\u662f\u4e00\u6b21\u6027\u7684\uff0c\u800c <code>StateFlow<\/code> \u4f1a\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u72b6\u6001\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8b\u4ef6\u88ab\u9519\u8bef\u5730\u91cd\u65b0\u5904\u7406\uff08\u4f8b\u5982\u5c4f\u5e55\u65cb\u8f6c\u540e\uff09\u3002<\/li>\n\n\n\n<li><code>SharedFlow<\/code> \u7684 <code>replay<\/code> \u548c\u7f13\u51b2\u914d\u7f6e\u63d0\u4f9b\u4e86\u5904\u7406\u4e8b\u4ef6\u573a\u666f\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4ece LiveData \u8fc1\u79fb\u5230 StateFlow\/SharedFlow \u7684\u8003\u91cf\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u547d\u5468\u671f\u7ba1\u7406\uff1a<\/strong> \u8fd9\u662f\u6700\u5927\u7684\u533a\u522b\u3002\u4f60\u9700\u8981\u786e\u4fdd\u5728 UI \u5c42\u4f7f\u7528 <code>lifecycleScope.launch { repeatOnLifecycle(...) { ... } }<\/code> \u6216 <code>flowWithLifecycle<\/code> \u6765\u5b89\u5168\u5730\u6536\u96c6 Flow\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u548c\u5728 UI \u4e0d\u53ef\u89c1\u65f6\u66f4\u65b0 UI\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\u5904\u7406\uff1a<\/strong> LiveData \u6709\u65f6\u4f1a\u88ab\u8bef\u7528\u4e8e\u5904\u7406\u4e8b\u4ef6\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\uff08\u4f8b\u5982\uff0c\u4e8b\u4ef6\u5728\u914d\u7f6e\u66f4\u6539\u540e\u88ab\u91cd\u65b0\u89e6\u53d1\uff09\u3002<code>SharedFlow<\/code> \u662f\u5904\u7406\u8fd9\u4e9b\u4e00\u6b21\u6027\u4e8b\u4ef6\u7684\u66f4\u597d\u9009\u62e9\u3002\u5982\u679c\u4f60\u4e4b\u524d\u7528 <code>SingleLiveEvent<\/code> \u8fd9\u6837\u7684\u6a21\u5f0f\uff0c<code>SharedFlow<\/code> \u53ef\u4ee5\u5f88\u597d\u5730\u66ff\u4ee3\u5b83\u3002<\/li>\n\n\n\n<li><strong>\u64cd\u4f5c\u7b26\uff1a<\/strong> Flow \u63d0\u4f9b\u4e86\u6bd4 LiveData <code>Transformations<\/code> \u66f4\u5f3a\u5927\u548c\u4e30\u5bcc\u7684\u64cd\u4f5c\u7b26\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8f6c\u6362\u548c\u7ec4\u5408\u6570\u636e\u6d41\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6d4b\u8bd5\u6027\uff1a<\/strong> \u4f7f\u7528 <code>kotlinx-coroutines-test<\/code> \u6d4b\u8bd5\u534f\u7a0b\u548c Flow \u901a\u5e38\u6bd4\u6d4b\u8bd5 LiveData \u66f4\u76f4\u63a5\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\uff0c<code>StateFlow<\/code> \u548c <code>SharedFlow<\/code> \u4e3a Android \u5f00\u53d1\u5e26\u6765\u4e86\u66f4\u73b0\u4ee3\u5316\u3001\u66f4\u5f3a\u5927\u3001\u66f4\u7075\u6d3b\u7684\u54cd\u5e94\u5f0f\u7f16\u7a0b\u5de5\u5177\uff0c\u5b83\u4eec\u662f LiveData \u7684\u6709\u529b\u66ff\u4ee3\u65b9\u6848\uff0c\u5c24\u5176\u662f\u5728\u4ee5 Kotlin \u4e3a\u4e3b\u7684\u534f\u7a0b\u9a71\u52a8\u7684\u67b6\u6784\u4e2d\u3002\u9009\u62e9\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff1a\u7528 <code>StateFlow<\/code> \u7ba1\u7406\u72b6\u6001\uff0c\u7528 <code>SharedFlow<\/code> \u5e7f\u64ad\u4e8b\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684\uff0c\u6211\u4eec\u6765\u63a2\u8ba8\u4e00\u4e0b\u57fa\u4e8e Kotlin \u534f\u7a0b\u7684 LiveData \u66ff\u4ee3\u65b9\u6848\uff0c\u8be6\u7ec6\u4e86\u89e3\u5b83\u4eec\u7684\u7279\u6027\uff0c\u5e76\u4e0e LiveData \u8fdb\u884c\u5bf9\u6bd4\u3002 \u5728 Android \u5f00\u53d1\u4e2d\uff0cLiveData \u4e00\u76f4\u662f\u54cd\u5e94\u5f0f UI \u7f16\u7a0b\u7684\u5e38\u7528\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u611f\u77e5\u751f\u547d\u5468\u671f\uff0c\u5e76\u4e14\u53ef\u4ee5\u5f88\u597d\u5730\u4e0e ViewModel \u914d\u5408\u4f7f\u7528\u3002\u7136\u800c\uff0c\u968f\u7740 Kotlin \u534f\u7a0b\u7684\u666e\u53ca\uff0c\u51fa\u73b0\u4e86\u529f\u80fd\u66f4\u5f3a\u5927\u3001\u66f4\u7075\u6d3b\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u7279\u522b\u662f StateFlow \u548c SharedFlow\u3002 LiveData \u66ff\u4ee3\u65b9\u6848\uff1aStateFlow \u548c SharedFlow StateFlow \u548c SharedFlow \u662f Kotlin \u534f\u7a0b\u5e93\u4e2d Flow API \u7684\u4e00\u90e8\u5206\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e0e LiveData \u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46\u5177\u6709\u534f\u7a0b\u7684\u5f3a\u5927\u4f18\u52bf\u548c\u7075\u6d3b\u6027\u3002 1. StateFlow StateFlow \u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684 SharedFlow\uff0c\u5b83\u8868\u793a\u4e00\u4e2a\u53ef\u89c2\u5bdf\u7684\u72b6\u6001\u5bb9\u5668\u3002\u5b83\u603b\u662f\u6709\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u5e76\u4e14\u53ea\u53d1\u51fa\u6700\u65b0\u7684\u72b6\u6001\u66f4\u65b0\u3002\u5982\u679c\u4f60\u719f\u6089 RxJava\uff0cStateFlow \u7c7b\u4f3c\u4e8e BehaviorSubject \u6216 LiveData \u672c\u8eab\u3002 \u7279\u6027\u8be6\u89e3\uff1a \u5982\u4f55\u4f7f\u7528 MutableStateFlow (\u5728 ViewModel \u4e2d): Kotlin \u5982\u4f55\u5728 UI \u5c42 (Activity\/Fragment) \u6536\u96c6 StateFlow: Kotlin 2. SharedFlow SharedFlow \u662f\u4e00\u79cd\u66f4\u901a\u7528\u7684\u70ed\u6570\u636e\u6d41\uff0c\u5b83\u53ef\u4ee5\u5c06\u53d1\u51fa\u7684\u503c\u5e7f\u64ad\u7ed9\u591a\u4e2a\u6536\u96c6\u5668\u3002\u4e0e StateFlow \u4e0d\u540c\uff0cSharedFlow \u4e0d\u4e00\u5b9a\u6709\u521d\u59cb\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u914d\u7f6e\u5176\u91cd\u64ad\u7f13\u5b58\uff08replay cache\uff09\u548c\u7f13\u51b2\u7b56\u7565\u3002 \u7279\u6027\u8be6\u89e3\uff1a \u5982\u4f55\u4f7f\u7528 MutableSharedFlow (\u5728 ViewModel \u4e2d\uff0c\u7528\u4e8e\u4e00\u6b21\u6027\u4e8b\u4ef6): Kotlin \u5982\u4f55\u5728 UI \u5c42 (Activity\/Fragment) \u6536\u96c6 SharedFlow: Kotlin LiveData vs. StateFlow vs. SharedFlow \u5bf9\u6bd4 \u7279\u6027 LiveData StateFlow SharedFlow \u4e3b\u8981\u7528\u9014 UI \u72b6\u6001\u6301\u6709\uff0c\u6570\u636e\u89c2\u5bdf UI \u72b6\u6001\u6301\u6709\uff0c\u5355\u4e00\u6700\u65b0\u72b6\u6001\u5e7f\u64ad \u4e8b\u4ef6\u5e7f\u64ad\uff0c\u591a\u4e2a\u6536\u96c6\u5668\u5171\u4eab\u6570\u636e\u6d41 \u521d\u59cb\u503c \u53ef\u4ee5\u6ca1\u6709\uff08\u4f46\u901a\u5e38\u6709\uff09 \u5fc5\u987b\u6709 \u53ef\u4ee5\u6ca1\u6709 \u503c\u7c7b\u578b \u4efb\u4f55\u7c7b\u578b \u4efb\u4f55\u7c7b\u578b \u4efb\u4f55\u7c7b\u578b \u611f\u77e5\u751f\u547d\u5468\u671f \u5185\u7f6e (\u81ea\u52a8\u5904\u7406\u8ba2\u9605\u548c\u53d6\u6d88\u8ba2\u9605) \u4e0d\u5185\u7f6e (\u9700\u914d\u5408 lifecycleScope.repeatOnLifecycle \u6216 flowWithLifecycle) \u4e0d\u5185\u7f6e (\u9700\u914d\u5408 lifecycleScope.repeatOnLifecycle \u6216 flowWithLifecycle) \u6570\u636e\u6d41\u7c7b\u578b \u70ed\u6570\u636e\u6d41 \u70ed\u6570\u636e\u6d41 \u70ed\u6570\u636e\u6d41 \u503c\u5408\u5e76 (Conflation) \u53ea\u53d1\u9001\u6700\u65b0\u503c (\u5982\u679c Activity\/Fragment \u4e0d\u6d3b\u8dc3\u5219\u4e0d\u53d1\u9001\uff0c\u6d3b\u8dc3\u540e\u53d1\u9001\u6700\u65b0\u503c) \u53ea\u53d1\u9001\u6700\u65b0\u503c (\u6536\u96c6\u5668\u603b\u662f\u83b7\u5f97\u6700\u65b0\u72b6\u6001) \u53ef\u914d\u7f6e (\u901a\u8fc7 replay \u548c\u7f13\u51b2\u7b56\u7565\uff0c\u9ed8\u8ba4\u4e0d\u91cd\u64ad\u65e7\u503c) \u53ef\u53d8\u6027 MutableLiveData MutableStateFlow MutableSharedFlow \u534f\u7a0b\u96c6\u6210 \u6709\u9650 (\u901a\u8fc7 liveData \u6784\u5efa\u5668) \u539f\u751f\u96c6\u6210 (Kotlin \u534f\u7a0b\u7684\u4e00\u90e8\u5206) \u539f\u751f\u96c6\u6210 (Kotlin \u534f\u7a0b\u7684\u4e00\u90e8\u5206) \u80cc\u538b\u5904\u7406 \u81ea\u52a8\u5904\u7406 (\u56e0\u4e3a\u751f\u547d\u5468\u671f\u611f\u77e5\u548c\u503c\u5408\u5e76) \u5929\u7136\u5904\u7406 (\u56e0\u53ea\u5173\u5fc3\u6700\u65b0\u72b6\u6001) \u53ef\u901a\u8fc7\u7f13\u51b2\u7b56\u7565\u914d\u7f6e \u7ebf\u7a0b \u4e3b\u7ebf\u7a0b\u66f4\u65b0 (\u901a\u8fc7 setValue)\uff0c\u540e\u53f0\u7ebf\u7a0b\u66f4\u65b0 (\u901a\u8fc7 postValue) value \u66f4\u65b0\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6536\u96c6\u901a\u5e38\u5728\u6307\u5b9a CoroutineContext \u8fdb\u884c emit \u662f\u6302\u8d77\u51fd\u6570\uff0ctryEmit \u662f\u975e\u6302\u8d77\u51fd\u6570\uff1b\u6536\u96c6\u5728\u6307\u5b9a CoroutineContext \u8fdb\u884c \u64cd\u4f5c\u7b26 \u6709\u9650\u7684\u8f6c\u6362\u64cd\u4f5c (Transformations.map, switchMap) \u4e30\u5bcc\u7684 Flow \u64cd\u4f5c\u7b26 (map, filter, combine \u7b49) \u4e30\u5bcc\u7684 Flow \u64cd\u4f5c\u7b26 (map, filter, shareIn \u7b49) \u6d4b\u8bd5 \u9700\u8981\u7279\u5b9a\u89c4\u5219 (\u5982 InstantTaskExecutorRule) \u4f7f\u7528 kotlinx-coroutines-test \u4f7f\u7528 kotlinx-coroutines-test \u9002\u7528\u573a\u666f \u7b80\u5355\u7684 UI \u72b6\u6001\u7ba1\u7406 \u590d\u6742\u7684 UI \u72b6\u6001\u7ba1\u7406\uff0c\u9700\u8981\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\u548c Flow \u64cd\u4f5c\u7b26 \u4e00\u6b21\u6027\u4e8b\u4ef6 (Snackbar, \u5bfc\u822a)\uff0c\u9700\u8981\u5e7f\u64ad\u7ed9\u591a\u4e2a\u8ba2\u9605\u8005\u7684\u573a\u666f\uff0c\u9700\u8981\u7f13\u5b58\u548c\u91cd\u64ad\u7684\u573a\u666f Java \u4e92\u64cd\u4f5c\u6027 \u826f\u597d \u826f\u597d (\u4f46\u66f4\u504f\u5411 Kotlin) \u826f\u597d (\u4f46\u66f4\u504f\u5411 Kotlin) \u7a7a\u5b89\u5168 \u9700\u8981\u6ce8\u610f\u53ef\u7a7a\u6027 Kotlin \u539f\u751f\u652f\u6301\u7a7a\u5b89\u5168 Kotlin \u539f\u751f\u652f\u6301\u7a7a\u5b89\u5168 \u603b\u7ed3\u4e0e\u9009\u62e9\u5efa\u8bae \u4ece LiveData \u8fc1\u79fb\u5230 StateFl&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[61],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-appdev","tag-kotlin"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}