{"id":475,"date":"2025-04-15T15:45:58","date_gmt":"2025-04-15T07:45:58","guid":{"rendered":"https:\/\/trantor.ink\/?p=475"},"modified":"2025-04-15T15:45:59","modified_gmt":"2025-04-15T07:45:59","slug":"kotlin-%e5%8d%8f%e7%a8%8b-flow-api-%e4%b8%ad%e7%9a%84%e5%86%b7%e6%b5%81%e5%92%8c%e7%83%ad%e6%b5%81","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=475","title":{"rendered":"Kotlin \u534f\u7a0b Flow Api \u4e2d\u7684\u51b7\u6d41\u548c\u70ed\u6d41"},"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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=475\/#%F0%9F%94%A5_%E7%83%AD%E6%B5%81%EF%BC%88Hot_Flow%EF%BC%89\" >\ud83d\udd25 \u70ed\u6d41\uff08Hot Flow\uff09<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9C%85_%E5%AE%9A%E4%B9%89%EF%BC%9A\" >\u2705 \u5b9a\u4e49\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9C%85_%E7%89%B9%E7%82%B9%EF%BC%9A\" >\u2705 \u7279\u70b9\uff1a<\/a><\/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=475\/#%E2%9C%85_%E7%A4%BA%E4%BE%8B%EF%BC%9AStateFlow\" >\u2705 \u793a\u4f8b\uff1aStateFlow<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9D%84%EF%B8%8F_%E5%86%B7%E6%B5%81%EF%BC%88Cold_Flow%EF%BC%89\" >\u2744\ufe0f \u51b7\u6d41\uff08Cold Flow\uff09<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9C%85_%E5%AE%9A%E4%B9%89%EF%BC%9A-2\" >\u2705 \u5b9a\u4e49\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9C%85_%E7%89%B9%E7%82%B9%EF%BC%9A-2\" >\u2705 \u7279\u70b9\uff1a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/trantor.ink\/?p=475\/#%E2%9C%85_%E7%A4%BA%E4%BE%8B%EF%BC%9A%E6%99%AE%E9%80%9A_Flow\" >\u2705 \u793a\u4f8b\uff1a\u666e\u901a Flow<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/trantor.ink\/?p=475\/#%F0%9F%94%8D_%E7%83%AD%E6%B5%81_vs_%E5%86%B7%E6%B5%81%EF%BC%9A%E5%AF%B9%E6%AF%94%E8%A1%A8\" >\ud83d\udd0d \u70ed\u6d41 vs \u51b7\u6d41\uff1a\u5bf9\u6bd4\u8868<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/trantor.ink\/?p=475\/#%F0%9F%8E%AF_%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%EF%BC%9F\" >\ud83c\udfaf \u5982\u4f55\u9009\u62e9\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/trantor.ink\/?p=475\/#%F0%9F%A7%A0_%E8%AE%B0%E5%BF%86%E5%B0%8F%E6%8A%80%E5%B7%A7\" >\ud83e\udde0 \u8bb0\u5fc6\u5c0f\u6280\u5de7<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>\u5f53\u7136\uff01<strong>\u201c\u51b7\u6d41\u201d\u548c\u201c\u70ed\u6d41\u201d<\/strong> \u662f\u5728\u4f7f\u7528 Kotlin \u534f\u7a0b Flow \u7cfb\u5217 API \u65f6\u975e\u5e38\u6838\u5fc3\u7684\u4e24\u4e2a\u6982\u5ff5\uff0c\u5b83\u4eec\u5f71\u54cd\u4f60\u5982\u4f55\u83b7\u53d6\u6570\u636e\u3001\u4f55\u65f6\u5f00\u59cb\u3001\u662f\u5426\u4f1a\u91cd\u590d\u6267\u884c\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%94%A5_%E7%83%AD%E6%B5%81%EF%BC%88Hot_Flow%EF%BC%89\"><\/span><strong>\ud83d\udd25 \u70ed\u6d41\uff08Hot Flow\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E5%AE%9A%E4%B9%89%EF%BC%9A\"><\/span><strong>\u2705 \u5b9a\u4e49\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u70ed\u6d41\uff08Hot Flow\uff09<strong>\u59cb\u7ec8\u5728\u53d1\u51fa\u6570\u636e<\/strong>\uff0c\u65e0\u8bba\u6709\u6ca1\u6709\u89c2\u5bdf\u8005\uff08\u8ba2\u9605\u8005\uff09\u5728\u76d1\u542c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E7%89%B9%E7%82%B9%EF%BC%9A\"><\/span><strong>\u2705 \u7279\u70b9\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u7279\u6027<\/strong><\/th><th><strong>\u63cf\u8ff0<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u72ec\u7acb\u4e8e\u89c2\u5bdf\u8005<\/td><td>\u6570\u636e\u6e90\u4e00\u76f4\u5728\u5de5\u4f5c\uff0c\u5373\u4f7f\u6ca1\u6709\u4eba\u6536\u96c6<\/td><\/tr><tr><td>\u4e0d\u4f1a\u91cd\u65b0\u89e6\u53d1<\/td><td>\u65b0\u89c2\u5bdf\u8005\u53ea\u80fd\u63a5\u6536\u5230\u5f53\u524d\u6216\u672a\u6765\u7684\u6570\u636e\uff0c\u65e0\u6cd5\u770b\u5230\u8fc7\u53bb\u7684\u53d1\u5c04<\/td><\/tr><tr><td>\u5e38\u89c1\u7c7b\u578b<\/td><td>StateFlow, SharedFlow, Channel, LiveData<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E7%A4%BA%E4%BE%8B%EF%BC%9AStateFlow\"><\/span><strong>\u2705 \u793a\u4f8b\uff1aStateFlow<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\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=\"\">val stateFlow = MutableStateFlow(0)\n\nstateFlow.value = 1\nstateFlow.value = 2\n\nlifecycleScope.launch {\n    stateFlow.collect {\n        println(\"\u6536\u5230\uff1a$it\") \/\/ \u6536\u5230\u7684\u662f 2\n    }\n}<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u5373\u4fbf\u4f60 collect \u6bd4\u8f83\u665a\u4e86\uff0c\u4e5f\u80fd\u9a6c\u4e0a\u6536\u5230\u201c\u5f53\u524d\u72b6\u6001\u201d\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9D%84%EF%B8%8F_%E5%86%B7%E6%B5%81%EF%BC%88Cold_Flow%EF%BC%89\"><\/span><strong>\u2744\ufe0f \u51b7\u6d41\uff08Cold Flow\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E5%AE%9A%E4%B9%89%EF%BC%9A-2\"><\/span><strong>\u2705 \u5b9a\u4e49\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u51b7\u6d41\uff08Cold Flow\uff09\u53ea\u6709\u5728\u88ab<strong>\u6536\u96c6\uff08collect\uff09\u65f6\u624d\u5f00\u59cb\u53d1\u5c04\u6570\u636e<\/strong>\uff0c\u6bcf\u6b21 collect \u90fd\u4f1a<strong>\u91cd\u65b0\u6267\u884c<\/strong>\u6d41\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E7%89%B9%E7%82%B9%EF%BC%9A-2\"><\/span><strong>\u2705 \u7279\u70b9\uff1a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u7279\u6027<\/strong><\/th><th><strong>\u63cf\u8ff0<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u60f0\u6027\u542f\u52a8<\/td><td>\u6ca1\u6709\u8ba2\u9605\u8005\u65f6\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5<\/td><\/tr><tr><td>\u6bcf\u6b21 collect \u90fd\u91cd\u65b0\u6267\u884c<\/td><td>\u50cf\u51fd\u6570\u4e00\u6837\uff0c\u6709\u8f93\u5165\u5c31\u6709\u6267\u884c<\/td><\/tr><tr><td>\u9002\u5408\u4e00\u6b21\u6027\u4efb\u52a1<\/td><td>\u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u5e93\u67e5\u8be2<\/td><\/tr><tr><td>\u5e38\u89c1\u7c7b\u578b<\/td><td>Flow\uff08\u9ed8\u8ba4\u5c31\u662f\u51b7\u6d41\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E2%9C%85_%E7%A4%BA%E4%BE%8B%EF%BC%9A%E6%99%AE%E9%80%9A_Flow\"><\/span><strong>\u2705 \u793a\u4f8b\uff1a\u666e\u901a Flow<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\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=\"\">val flow = flow {\n    println(\"flow \u88ab\u89e6\u53d1\")\n    emit(\"Hello\")\n}\n\nlifecycleScope.launch {\n    flow.collect {\n        println(\"\u6536\u5230\uff1a$it\")\n    }\n}<\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/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=\"\">flow \u88ab\u89e6\u53d1\n\u6536\u5230\uff1aHello<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u6bcf\u6b21 collect\uff0c\u90fd\u4f1a\u91cd\u65b0\u6267\u884c flow { &#8230; } \u91cc\u7684\u5185\u5bb9\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%94%8D_%E7%83%AD%E6%B5%81_vs_%E5%86%B7%E6%B5%81%EF%BC%9A%E5%AF%B9%E6%AF%94%E8%A1%A8\"><\/span><strong>\ud83d\udd0d \u70ed\u6d41 vs \u51b7\u6d41\uff1a\u5bf9\u6bd4\u8868<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u7279\u6027<\/strong><\/th><th><strong>\u51b7\u6d41 (<\/strong>Flow<strong>)<\/strong><\/th><th><strong>\u70ed\u6d41 (<\/strong>StateFlow<strong>, <\/strong>SharedFlow<strong>)<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u662f\u5426\u7acb\u5373\u5f00\u59cb<\/td><td>\u274c \u5426\uff0ccollect \u624d\u5f00\u59cb<\/td><td>\u2705 \u662f\uff0c\u521b\u5efa\u540e\u5c31\u5f00\u59cb\u53d1\u5c04<\/td><\/tr><tr><td>\u591a\u6b21\u6536\u96c6\u662f\u5426\u91cd\u65b0\u6267\u884c<\/td><td>\u2705 \u662f<\/td><td>\u274c \u5426\uff0c\u53ea\u80fd\u62ff\u5230\u6700\u65b0\u503c\uff08StateFlow\uff09\u6216\u4e8b\u4ef6\uff08SharedFlow\uff09<\/td><\/tr><tr><td>\u662f\u5426\u4fdd\u7559\u6700\u65b0\u503c<\/td><td>\u274c \u5426<\/td><td>\u2705 \u662f\uff08StateFlow\uff09<\/td><\/tr><tr><td>\u9002\u5408\u505a\u4ec0\u4e48<\/td><td>\u7f51\u7edc\u8bf7\u6c42\u3001DB\u3001\u4e00\u6b21\u6027\u4efb\u52a1<\/td><td>UI \u72b6\u6001\u3001\u4e8b\u4ef6\u901a\u77e5<\/td><\/tr><tr><td>\u662f\u5426\u652f\u6301\u751f\u547d\u5468\u671f\u611f\u77e5<\/td><td>\u274c \u4e0d\u81ea\u52a8\u652f\u6301<\/td><td>\u274c \u4e0d\u81ea\u52a8\u652f\u6301\uff08\u4f46\u53ef\u7ed3\u5408 repeatOnLifecycle\uff09<\/td><\/tr><tr><td>\u7c7b\u6bd4<\/td><td>\u51fd\u6570<\/td><td>\u5e7f\u64ad\u3001\u72b6\u6001\u673a<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%8E%AF_%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%EF%BC%9F\"><\/span><strong>\ud83c\udfaf \u5982\u4f55\u9009\u62e9\uff1f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u573a\u666f<\/strong><\/th><th><strong>\u63a8\u8350<\/strong><\/th><\/tr><\/thead><tbody><tr><td>\u9700\u8981\u6301\u7eed\u76d1\u542c\u72b6\u6001\u3001\u4fdd\u5b58\u6700\u540e\u4e00\u4e2a\u503c<\/td><td>StateFlow<\/td><\/tr><tr><td>\u4e00\u6b21\u6027\u4e8b\u4ef6\u3001\u591a\u89c2\u5bdf\u8005\u8ba2\u9605<\/td><td>SharedFlow<\/td><\/tr><tr><td>\u8bf7\u6c42 API \u6216\u6570\u636e\u5e93<\/td><td>Flow\uff08\u51b7\u6d41\uff09<\/td><\/tr><tr><td>\u590d\u6742\u4efb\u52a1\u8c03\u5ea6<\/td><td>Channel<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%A7%A0_%E8%AE%B0%E5%BF%86%E5%B0%8F%E6%8A%80%E5%B7%A7\"><\/span><strong>\ud83e\udde0 \u8bb0\u5fc6\u5c0f\u6280\u5de7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>| \u51b7\u6d41 | \u50cf\u51fd\u6570\uff1a\u4f60\u8c03\u6211\u4e00\u6b21\uff0c\u6211\u5c31\u6267\u884c\u4e00\u6b21 |<\/p>\n\n\n\n<p>| \u70ed\u6d41 | \u50cf\u5e7f\u64ad\u7ad9\uff1a\u6211\u4e00\u76f4\u5728\u53d1\uff0c\u4f60\u6765\u6211\u5c31\u64ad\u7ed9\u4f60 |<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u7136\uff01\u201c\u51b7\u6d41\u201d\u548c\u201c\u70ed\u6d41\u201d \u662f\u5728\u4f7f\u7528 Kotlin \u534f\u7a0b Flow \u7cfb\u5217 API \u65f6\u975e\u5e38\u6838\u5fc3\u7684\u4e24\u4e2a\u6982\u5ff5\uff0c\u5b83\u4eec\u5f71\u54cd\u4f60\u5982\u4f55\u83b7\u53d6\u6570\u636e\u3001\u4f55\u65f6\u5f00\u59cb\u3001\u662f\u5426\u4f1a\u91cd\u590d\u6267\u884c\u7b49\u3002 \ud83d\udd25 \u70ed\u6d41\uff08Hot Flow\uff09 \u2705 \u5b9a\u4e49\uff1a \u70ed\u6d41\uff08Hot Flow\uff09\u59cb\u7ec8\u5728\u53d1\u51fa\u6570\u636e\uff0c\u65e0\u8bba\u6709\u6ca1\u6709\u89c2\u5bdf\u8005\uff08\u8ba2\u9605\u8005\uff09\u5728\u76d1\u542c\u3002 \u2705 \u7279\u70b9\uff1a \u7279\u6027 \u63cf\u8ff0 \u72ec\u7acb\u4e8e\u89c2\u5bdf\u8005 \u6570\u636e\u6e90\u4e00\u76f4\u5728\u5de5\u4f5c\uff0c\u5373\u4f7f\u6ca1\u6709\u4eba\u6536\u96c6 \u4e0d\u4f1a\u91cd\u65b0\u89e6\u53d1 \u65b0\u89c2\u5bdf\u8005\u53ea\u80fd\u63a5\u6536\u5230\u5f53\u524d\u6216\u672a\u6765\u7684\u6570\u636e\uff0c\u65e0\u6cd5\u770b\u5230\u8fc7\u53bb\u7684\u53d1\u5c04 \u5e38\u89c1\u7c7b\u578b StateFlow, SharedFlow, Channel, LiveData \u2705 \u793a\u4f8b\uff1aStateFlow \u2705 \u5373\u4fbf\u4f60 collect \u6bd4\u8f83\u665a\u4e86\uff0c\u4e5f\u80fd\u9a6c\u4e0a\u6536\u5230\u201c\u5f53\u524d\u72b6\u6001\u201d\u3002 \u2744\ufe0f \u51b7\u6d41\uff08Cold Flow\uff09 \u2705 \u5b9a\u4e49\uff1a \u51b7\u6d41\uff08Cold Flow\uff09\u53ea\u6709\u5728\u88ab\u6536\u96c6\uff08collect\uff09\u65f6\u624d\u5f00\u59cb\u53d1\u5c04\u6570\u636e\uff0c\u6bcf\u6b21 collect \u90fd\u4f1a\u91cd\u65b0\u6267\u884c\u6d41\u7684\u903b\u8f91\u3002 \u2705 \u7279\u70b9\uff1a \u7279\u6027 \u63cf\u8ff0 \u60f0\u6027\u542f\u52a8 \u6ca1\u6709\u8ba2\u9605\u8005\u65f6\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5 \u6bcf\u6b21 collect \u90fd\u91cd\u65b0\u6267\u884c \u50cf\u51fd\u6570\u4e00\u6837\uff0c\u6709\u8f93\u5165\u5c31\u6709\u6267\u884c \u9002\u5408\u4e00\u6b21\u6027\u4efb\u52a1 \u6bd4\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u5e93\u67e5\u8be2 \u5e38\u89c1\u7c7b\u578b Flow\uff08\u9ed8\u8ba4\u5c31\u662f\u51b7\u6d41\uff09 \u2705 \u793a\u4f8b\uff1a\u666e\u901a Flow \u8f93\u51fa\uff1a \u2705 \u6bcf\u6b21 collect\uff0c\u90fd\u4f1a\u91cd\u65b0\u6267\u884c flow { &#8230; } \u91cc\u7684\u5185\u5bb9\u3002 \ud83d\udd0d \u70ed\u6d41 vs \u51b7\u6d41\uff1a\u5bf9\u6bd4\u8868 \u7279\u6027 \u51b7\u6d41 (Flow) \u70ed\u6d41 (StateFlow, SharedFlow) \u662f\u5426\u7acb\u5373\u5f00\u59cb \u274c \u5426\uff0ccollect \u624d\u5f00\u59cb \u2705 \u662f\uff0c\u521b\u5efa\u540e\u5c31\u5f00\u59cb\u53d1\u5c04 \u591a\u6b21\u6536\u96c6\u662f\u5426\u91cd\u65b0\u6267\u884c \u2705 \u662f \u274c \u5426\uff0c\u53ea\u80fd\u62ff\u5230\u6700\u65b0\u503c\uff08StateFlow\uff09\u6216\u4e8b\u4ef6\uff08SharedFlow\uff09 \u662f\u5426\u4fdd\u7559\u6700\u65b0\u503c \u274c \u5426 \u2705 \u662f\uff08StateFlow\uff09 \u9002\u5408\u505a\u4ec0\u4e48 \u7f51\u7edc\u8bf7\u6c42\u3001DB\u3001\u4e00\u6b21\u6027\u4efb\u52a1 UI \u72b6\u6001\u3001\u4e8b\u4ef6\u901a\u77e5 \u662f\u5426\u652f\u6301\u751f\u547d\u5468\u671f\u611f\u77e5 \u274c \u4e0d\u81ea\u52a8\u652f\u6301 \u274c \u4e0d\u81ea\u52a8\u652f\u6301\uff08\u4f46\u53ef\u7ed3\u5408 repeatOnLifecycle\uff09 \u7c7b\u6bd4 \u51fd\u6570 \u5e7f\u64ad\u3001\u72b6\u6001\u673a \ud83c\udfaf \u5982\u4f55\u9009\u62e9\uff1f \u573a\u666f \u63a8\u8350 \u9700\u8981\u6301\u7eed\u76d1\u542c\u72b6\u6001\u3001\u4fdd\u5b58\u6700\u540e\u4e00\u4e2a\u503c StateFlow \u4e00\u6b21\u6027\u4e8b\u4ef6\u3001\u591a\u89c2\u5bdf\u8005\u8ba2\u9605 SharedFlow \u8bf7\u6c42 API \u6216\u6570\u636e\u5e93 Flow\uff08\u51b7\u6d41\uff09 \u590d\u6742\u4efb\u52a1\u8c03\u5ea6 Channel \ud83e\udde0 \u8bb0\u5fc6\u5c0f\u6280\u5de7 | \u51b7\u6d41 | \u50cf\u51fd\u6570\uff1a\u4f60\u8c03\u6211\u4e00\u6b21\uff0c\u6211\u5c31\u6267\u884c\u4e00\u6b21 | | \u70ed\u6d41 | \u50cf\u5e7f\u64ad\u7ad9\uff1a\u6211\u4e00\u76f4\u5728\u53d1\uff0c\u4f60\u6765\u6211\u5c31\u64ad\u7ed9\u4f60 |<\/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":[],"class_list":["post-475","post","type-post","status-publish","format-standard","hentry","category-appdev"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/475","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=475"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":476,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/475\/revisions\/476"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}