{"id":783,"date":"2025-07-21T13:45:42","date_gmt":"2025-07-21T13:45:42","guid":{"rendered":"https:\/\/boochlin.com\/?p=783"},"modified":"2025-07-21T13:48:32","modified_gmt":"2025-07-21T13:48:32","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 class=\"wp-block-heading\">\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<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ In NewsRepository.kt\nclass NewsRepository(\n    private val newsApi: NewsApiService,\n    \/\/ \u7531\u5916\u90e8 DI \u6846\u67b6\u63d0\u4f9b\u4e00\u500b Application-Scope\n    private val externalScope: CoroutineScope\n) {\n    private var cachedNews: List&lt;News> = emptyList()\n    private val mutex = Mutex()\n\n    suspend fun getLatestNews(): List&lt;News> {\n        \/\/ \u4f7f\u7528 async \u5c07\u4efb\u52d9\u767c\u5c04\u5230\u5916\u90e8 Scope\uff0c\u4e26\u7528 await \u7b49\u5f85\u7d50\u679c\n        return externalScope.async {\n            val result = newsApi.fetchLatest()\n            mutex.withLock { cachedNews = result }\n            result\n        }.await()\n    }\n}\n<\/pre>\n<h4 class=\"wp-block-heading\">\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<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ In NewsRepository.kt\nclass NewsRepository(...) {\n    private val _newsFlow = MutableStateFlow&lt;List&lt;News>>(emptyList())\n    val newsFlow: StateFlow&lt;List&lt;News>> = _newsFlow\n\n    \/\/ \u5237\u65b0\u65b9\u6cd5\u8b8a\u6210\u5c04\u5f8c\u4e0d\u7406\n    fun refreshNews() {\n        externalScope.launch {\n            val result = newsApi.fetchLatest()\n            _newsFlow.value = result \/\/ \u66f4\u65b0\u8cc7\u6599\u6d41\uff0cUI \u6703\u81ea\u52d5\u6536\u5230\n        }\n    }\n}\n\n\/\/ In NewsViewModel.kt\nclass NewsViewModel(private val repo: NewsRepository) : ViewModel() {\n    val news = repo.newsFlow \/\/ \u76f4\u63a5\u89c0\u5bdf Flow\n    fun onRefresh() {\n        repo.refreshNews() \/\/ \u53ea\u9700\u89f8\u767c\uff0c\u4e0d\u7528\u7b49\u5f85\n    }\n}\n<\/pre><\/p>\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\u3002Repository \u63a5\u53d7\u4e00\u500b\u9577\u58fd\u7684 externalScope\uff0c\u7528 async \u628a\u4efb\u52d9\u4e1f\u9032\u53bb\u7368&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_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":4,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/797"}],"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}]}}