{"id":783,"date":"2025-07-21T13:45:42","date_gmt":"2025-07-21T13:45:42","guid":{"rendered":"https:\/\/boochlin.com\/?p=783"},"modified":"2026-09-19T17:04:10","modified_gmt":"2026-09-19T17:04:10","slug":"handle-all-repostiory-operation-that-out-of-composable-viewmodel-scope","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=783","title":{"rendered":"handle all repostiory operation  that out of Composable\/ViewModel scope"},"content":{"rendered":"<h4>\u4f5c\u6cd5\u4e00\uff1aasync + await\uff08\u6211\u8981\u7d50\u679c\uff0c\u4f46\u4efb\u52d9\u4e0d\u80fd\u505c\uff09<\/h4>\n<p>\u9019\u62db\u7684\u6838\u5fc3\u662f\u300c\u5206\u96e2\u57f7\u884c\u8207\u7b49\u5f85\u7684\u751f\u547d\u9031\u671f\u300d\u3002Repository \u63a5\u53d7\u4e00\u500b\u9577\u58fd\u7684 externalScope\uff0c\u7528 async \u628a\u4efb\u52d9\u4e1f\u9032\u53bb\u7368\u7acb\u57f7\u884c\uff0cUI \u5c64\u5247\u7528 await \u7b49\u5f85\u7d50\u679c\u3002UI \u96e2\u958b\uff0cawait \u53d6\u6d88\uff0c\u4f46 async \u5340\u584a\u7684\u4efb\u52d9\u6703\u7e7c\u7e8c\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-comment\">\/\/ In NewsRepository.kt<\/span>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">NewsRepository<\/span>(\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">val<\/span> newsApi: NewsApiService,\n    <span class=\"hljs-comment\">\/\/ \u7531\u5916\u90e8 DI \u6846\u67b6\u63d0\u4f9b\u4e00\u500b Application-Scope<\/span>\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">val<\/span> externalScope: CoroutineScope\n) {\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">var<\/span> cachedNews: List&lt;News&gt; = emptyList()\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">val<\/span> mutex = Mutex()\n\n    <span class=\"hljs-keyword\">suspend<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">getLatestNews<\/span><span class=\"hljs-params\">()<\/span><\/span>: List&lt;News&gt; {\n        <span class=\"hljs-comment\">\/\/ \u4f7f\u7528 async \u5c07\u4efb\u52d9\u767c\u5c04\u5230\u5916\u90e8 Scope\uff0c\u4e26\u7528 await \u7b49\u5f85\u7d50\u679c<\/span>\n        <span class=\"hljs-keyword\">return<\/span> externalScope.async {\n            <span class=\"hljs-keyword\">val<\/span> result = newsApi.fetchLatest()\n            mutex.withLock { cachedNews = result }\n            result\n        }.await()\n    }\n}<\/code><\/pre>\n<h4>\u4f5c\u6cd5\u4e8c\uff1alaunch + Flow\uff08\u5c04\u5f8c\u4e0d\u7406\uff0c\u6211\u53ea\u89c0\u5bdf\uff09<\/h4>\n<p>\u8aaa\u5be6\u8a71\uff0casync\/await \u6709\u9ede\u50cf\u5728\u9580\u53e3\u4e7e\u7b49\u5916\u9001\u3002\u5728\u591a\u6578\u60c5\u6cc1\u4e0b\uff0c\u4e00\u500b\u66f4\u512a\u96c5\u3001\u66f4\u89e3\u8026\u7684\u4f5c\u6cd5\u662f\u300c\u5c04\u5f8c\u4e0d\u7406\u300d\u3002UI \u53ea\u8ca0\u8cac\u89f8\u767c\u5237\u65b0\uff0c\u7136\u5f8c\u6301\u7e8c\u89c0\u5bdf\u4e00\u500b\u8cc7\u6599\u6d41 (Flow) \u7684\u8b8a\u5316\u3002<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-comment\">\/\/ In NewsRepository.kt<\/span>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">NewsRepository<\/span>(...) {\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">val<\/span> _newsFlow = MutableStateFlow&lt;List&lt;News&gt;&gt;(emptyList())\n    <span class=\"hljs-keyword\">val<\/span> newsFlow: StateFlow&lt;List&lt;News&gt;&gt; = _newsFlow\n\n    <span class=\"hljs-comment\">\/\/ \u5237\u65b0\u65b9\u6cd5\u8b8a\u6210\u5c04\u5f8c\u4e0d\u7406<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">refreshNews<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n        externalScope.launch {\n            <span class=\"hljs-keyword\">val<\/span> result = newsApi.fetchLatest()\n            _newsFlow.value = result <span class=\"hljs-comment\">\/\/ \u66f4\u65b0\u8cc7\u6599\u6d41\uff0cUI \u6703\u81ea\u52d5\u6536\u5230<\/span>\n        }\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ In NewsViewModel.kt<\/span>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">NewsViewModel<\/span>(<span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">val<\/span> repo: NewsRepository) : ViewModel() {\n    <span class=\"hljs-keyword\">val<\/span> news = repo.newsFlow <span class=\"hljs-comment\">\/\/ \u76f4\u63a5\u89c0\u5bdf Flow<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">onRefresh<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n        repo.refreshNews() <span class=\"hljs-comment\">\/\/ \u53ea\u9700\u89f8\u767c\uff0c\u4e0d\u7528\u7b49\u5f85<\/span>\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u6cd5\u4e00\uff1aasync + await\uff08\u6211\u8981\u7d50\u679c\uff0c\u4f46\u4efb\u52d9\u4e0d\u80fd\u505c\uff09 \u9019\u62db\u7684\u6838\u5fc3\u662f\u300c\u5206\u96e2\u57f7\u884c\u8207\u7b49\u5f85\u7684\u751f\u547d\u9031\u671f\u300d\u3002Rep [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1226,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[186,199,203,281],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/783"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=783"}],"version-history":[{"count":7,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":1557,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/1557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1226"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}