{"id":672,"date":"2025-07-14T01:54:23","date_gmt":"2025-07-14T01:54:23","guid":{"rendered":"https:\/\/boochlin.com\/?p=672"},"modified":"2025-08-27T13:09:42","modified_gmt":"2025-08-27T13:09:42","slug":"job-%e5%a4%a7%e5%a4%a7%e5%a4%a7%e4%b8%8d%e5%90%8c-%ef%bc%8csupervisorjob-%e4%b8%8d%e5%b0%b1%e6%98%af-viewmodelscope%e5%97%8e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=672","title":{"rendered":"SupervisorJob \u4e0d\u5c31\u662f viewModelScope\u55ce\uff1f"},"content":{"rendered":"<p><strong>Job \u4e0d\u50c5\u50c5\u662f launch \u7684\u56de\u50b3\u503c\uff0c\u5b83\u662f\u6211\u5011\u7ba1\u7406 Coroutine \u7684\u300c\u9059\u63a7\u5668\u300d\u3002<\/strong><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h3 class=\"wp-block-heading\">Job \uff1a<\/h3>\n<p>Job \u662f\u6700\u57fa\u672c\u7684\u5354\u5b9a\uff0c\u5b83\u9075\u5faa\u56b4\u683c\u7684\u300c\u7d50\u69cb\u5316\u4f75\u767c\u300d\u539f\u5247\u3002<\/p>\n<ul>\n<li><strong>\u7279\u6027\uff1a\u300c\u4e00\u640d\u4ff1\u640d\u300d\u3002<\/strong> \u5728\u4e00\u500b\u7531\u6a19\u6e96 Job \u7ba1\u7406\u7684\u7bc4\u7587\u5167\uff0c\u4efb\u4f55\u4e00\u500b\u5b50 Coroutine \u7684\u5931\u6557\uff08\u62cb\u51fa\u672a\u8655\u7406\u7684\u4f8b\u5916\uff09\uff0c\u90fd\u6703\u50cf\u63a8\u5012\u7b2c\u4e00\u5f35\u9aa8\u724c\u4e00\u6a23\uff0c\u7acb\u523b\u5c0e\u81f4\u6574\u500b\u7236 Job \u548c\u5176\u4e0b\u6240\u6709\u5176\u4ed6\u7684\u5b50 Coroutine \u88ab\u9023\u9396\u53d6\u6d88\u3002<\/li>\n<\/ul>\n<h4 class=\"wp-block-heading\">\u804a\u5929 APP \u5834\u666f\uff1a\u4f7f\u7528\u8005\u767b\u5165\u521d\u59cb\u5316<\/h4>\n<p>\u4e00\u500b\u4f7f\u7528\u8005\u767b\u5165\u6d41\u7a0b\uff0c\u53ef\u80fd\u5305\u542b\u300c\u9a57\u8b49\u5e33\u5bc6\u300d\u3001\u300c\u8b80\u53d6\u500b\u4eba\u8cc7\u6599\u300d\u3001\u300c\u7372\u53d6\u597d\u53cb\u5217\u8868\u300d\u7b49\u591a\u500b\u975e\u540c\u6b65\u6b65\u9a5f\u3002\u9019\u4e9b\u6b65\u9a5f\u662f\u4e00\u500b\u4e0d\u53ef\u5206\u5272\u7684\u6574\u9ad4\uff0c\u4efb\u4f55\u4e00\u6b65\u5931\u6557\uff0c\u6574\u500b\u767b\u5165\u6d41\u7a0b\u5c31\u8a72\u7acb\u5373\u5ba3\u544a\u5931\u6557\uff0c<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h3 class=\"wp-block-heading\">SupervisorJob<\/h3>\n<p>SupervisorJob \u662f Job \u7684\u4e00\u500b\u7279\u6b8a\u8b8a\u9ad4\uff0c\u5b83\u6253\u7834\u4e86\u300c\u4e00\u640d\u4ff1\u640d\u300d\u7684\u898f\u5247\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f37\u7684\u97cc\u6027\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u50cf\u6210\u4e00\u4f4d\u7d93\u9a57\u8c50\u5bcc\u3001\u8655\u8b8a\u4e0d\u9a5a\u7684\u5de5\u5ee0\u76e3\u5de5\u3002<\/p>\n<ul>\n<li><strong>\u7279\u6027\uff1a\u300c\u5931\u6557\u9694\u96e2\u300d\u3002<\/strong> \u5728 SupervisorJob \u7684\u76e3\u7763\u4e0b\uff0c\u67d0\u500b\u5b50 Coroutine \u7684\u5931\u6557\u4e0d\u6703\u5411\u4e0a\u50b3\u905e\uff0c\u56e0\u6b64\u4e0d\u6703\u5f71\u97ff\u5230\u7236 Job \u6216\u5176\u4ed6\u7684\u5144\u5f1f Coroutine\u3002<\/li>\n<li><strong>\u9069\u7528\u5834\u666f\uff1a\u9577\u671f\u5171\u5b58\u3001\u9700\u8981\u7368\u7acb\u904b\u4f5c\u7684\u5143\u4ef6\u96c6\u5408\u3002<\/strong><\/li>\n<\/ul>\n<h4 class=\"wp-block-heading\">\u804a\u5929 APP \u5834\u666f\uff1a\u5ba2\u6236\u7aef\u6838\u5fc3\u670d\u52d9<\/h4>\n<p>\u4e00\u500b\u5ba2\u6236\u7aef\u7684\u9023\u7dda Session\uff0c\u9700\u8981\u540c\u6642\u8655\u7406\u300c\u63a5\u6536\u8a0a\u606f\u300d\u3001\u300c\u767c\u9001\u8a0a\u606f\u300d\u3001\u300c\u5fc3\u8df3\u7dad\u6301\u300d\u9019\u4e09\u500b\u5e38\u99d0\u4efb\u52d9\u3002\u6211\u5011\u7d55\u4e0d\u5e0c\u671b\u56e0\u70ba\u300c\u767c\u9001\u300d\u6642\u7684\u7db2\u8def\u6296\u52d5\u5931\u6557\uff0c\u5c31\u5c0e\u81f4\u66f4\u91cd\u8981\u7684\u300c\u63a5\u6536\u300d\u548c\u300c\u5fc3\u8df3\u300d\u4e5f\u8ddf\u8457\u88ab\u53d6\u6d88\u3002SupervisorJob \u5c31\u50cf\u90a3\u4f4d\u76e3\u5de5\uff0c\u4ed6\u78ba\u4fdd\u4e00\u689d\u751f\u7522\u7dda\u7684\u77ed\u66ab\u6545\u969c\u4e0d\u6703\u5c0e\u81f4\u6574\u500b\u5de5\u5ee0\u505c\u64fa\u3002<\/p>\n<h4 class=\"wp-block-heading\">viewModelScope \u7684\u79d8\u5bc6<\/h4>\n<p>\u300c\u9019\u500b\u6a21\u5f0f\u807d\u8d77\u4f86\u5f88\u68d2\uff0c\u4f46\u9019\u662f\u6211\u9700\u8981\u624b\u52d5\u8655\u7406\u7684\u7279\u6b8a\u60c5\u6cc1\u55ce\uff1f\u300d\u4e0d\u7528\uff0c\u5df2\u7d93\u5167\u5efa\u4e86<\/p>\n<p>\u4f60\u53ef\u80fd\u6703\u601d\u8003\uff1a\u300c\u5982\u679c\u6211\u8981\u9694\u96e2\uff0c\u5728 Scope \u4e2d\u5206\u5225\u555f\u52d5\u4e09\u500b\u7368\u7acb Job \u4e0d\u5c31\u597d\u4e86\u55ce\uff1f\u300d\u4f46\u63a5\u8457\u4f60\u6703\u767c\u73fe\uff0c\u9019\u6a23\u505a\u6703\u5c0e\u81f4<strong>\u707d\u96e3<\/strong>\u2014\u2014\u7576 Session \u7d50\u675f\u6642\uff0c\u4f60\u5fc5\u9808\u624b\u52d5\u53d6\u6d88\u6bcf\u4e00\u500b Job\uff0c\u975e\u5e38\u5bb9\u6613\u51fa\u932f\u4e26\u9020\u6210\u8cc7\u6e90\u6d29\u6f0f\u3002<\/p>\n<p>\u8fce\u4f86\u9813\u609f\uff1a<strong>viewModelScope \u5c31\u662f\u9019\u6a23\u8a2d\u8a08\u7684\uff01<\/strong><\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-code\"><code>\/\/ ViewModel.kt in androidx.lifecycle\nval ViewModel.viewModelScope: CoroutineScope\n    get() {\n        \/\/ ...\n        val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)\n        \/\/ ...\n        return scope\n    }\n<\/code><\/pre>\n<p>\u6c92\u932f\uff0cviewModelScope \u5929\u751f\u5c31\u662f\u4e00\u500b SupervisorJob\u3002Google \u7684\u5de5\u7a0b\u5e2b\u65e9\u5c31\u6599\u5230\uff0c\u5728\u4e00\u500b ViewModel \u4e2d\uff0c\u4f60\u6703\u555f\u52d5\u591a\u500b\u4e92\u76f8\u7368\u7acb\u7684 UI \u76f8\u95dc\u4efb\u52d9\uff0c\u800c\u4e00\u500b\u4efb\u52d9\u7684\u5931\u6557\u4e0d\u61c9\u8a72\u8b93\u6574\u500b\u756b\u9762\u908f\u8f2f\u5d29\u6f70\u3002<strong>\u6240\u4ee5\u6211\u5011\u8a72\u95dc\u6ce8\u7684\uff0c\u6b63\u662f\u5982\u4f55\u5229\u7528 SupervisorJob \u7684\u7279\u6027\u53bb\u5340\u9694\u5834\u666f\uff0c<\/strong><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<h3 class=\"wp-block-heading\">Deferred \u2014 \u9644\u5e36\u53d6\u9910\u529f\u80fd\u7684\u9059\u63a7\u5668<\/h3>\n<p>Deferred \u662f\u4e00\u7a2e\u66f4\u7279\u6b8a\u7684 Job\uff0c\u5b83\u627f\u8afe\u6703\u5728\u672a\u4f86\u56de\u50b3\u4e00\u500b\u7d50\u679c\u3002\u6b63\u5982\u5176\u6e90\u78bc\u6240\u5b9a\u7fa9\uff0cDeferred <strong>\u7e7c\u627f\u81ea Job<\/strong>\u3002<\/p>\n<pre class=\"wp-block-code\"><code>\/\/ public interface Deferred&lt;out T&gt; : Job { ... }\n<\/code><\/pre>\n<p>\u9019\u610f\u5473\u8457 Deferred \u672c\u8eab\u5c31\u662f\u4e00\u500b Job\uff0c\u4f46\u5b83\u984d\u5916\u63d0\u4f9b\u4e86\u4e00\u500b await() \u65b9\u6cd5\u4f86\u7372\u53d6\u7d50\u679c\u3002<\/p>\n<ul>\n<li><strong>\u4f55\u6642\u4f7f\u7528\uff1f<\/strong> \u7576\u4f60\u7684 Coroutine \u4e0d\u53ea\u662f\u300c\u505a\u4e00\u4ef6\u4e8b\u300d\uff0c\u800c\u662f\u8981\u300c<strong>\u7b97\u4e00\u500b\u7d50\u679c<\/strong>\u300d\u6642\uff0c\u5c31\u8a72\u7528 async \u555f\u52d5\u5668\uff0c\u5b83\u6703\u56de\u50b3\u4e00\u500b Deferred \u7269\u4ef6\u3002<\/li>\n<li>\u00a0Deferred \u5c31\u50cf\u4f60\u53bb\u9910\u5ef3\u9ede\u9910\u62ff\u5230\u7684<strong>\u865f\u78bc\u724c<\/strong>\u3002\u5b83\u672c\u8eab\u662f\u300c\u5e97\u5bb6\u6b63\u5728\u505a\u83dc\u300d\u7684\u6191\u8b49\uff08\u4e00\u500b Job\uff09\uff0c\u4f46\u66f4\u91cd\u8981\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u7a0d\u5f8c\u6191\u8457\u5b83\uff0c\u7528 .await() \u53bb\u53d6\u56de\u4f60\u7684\u9910\u9ede\uff08\u7d50\u679c\uff09\u3002<\/li>\n<\/ul>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Job \u4e0d\u50c5\u50c5\u662f launch \u7684\u56de\u50b3\u503c\uff0c\u5b83\u662f\u6211\u5011\u7ba1\u7406 Coroutine \u7684\u300c\u9059\u63a7\u5668\u300d\u3002 Job \uff1a Job \u662f\u6700\u57fa\u672c\u7684\u5354\u5b9a\uff0c\u5b83\u9075\u5faa\u56b4\u683c\u7684\u300c\u7d50\u69cb\u5316\u4f75\u767c\u300d\u539f\u5247\u3002 \u7279\u6027\uff1a\u300c\u4e00\u640d\u4ff1\u640d\u300d\u3002 \u5728\u4e00\u500b\u7531\u6a19\u6e96 J&#8230;<\/p>\n","protected":false},"author":1,"featured_media":864,"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\/672"}],"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=672"}],"version-history":[{"count":12,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/672\/revisions\/865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/864"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}