{"id":138041,"date":"2020-07-21T22:33:23","date_gmt":"2020-07-21T14:33:23","guid":{"rendered":"http:\/\/4563.org\/?p=138041"},"modified":"2020-07-21T22:33:23","modified_gmt":"2020-07-21T14:33:23","slug":"withcontext-%e4%b8%8e-suspend-%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f-2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=138041","title":{"rendered":"withContext \u4e0e suspend \u533a\u522b\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  withContext \u4e0e suspend \u533a\u522b\u662f\u4ec0\u4e48\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : andev <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        withContext \u662f\u6307\u5b9a\u534f\u7a0b\u8fd0\u884c\u7684\u7ebf\u7a0b\uff0csuspend \u662f\u6302\u8d77\uff0c\u90a3\u8bf7\u6c42\u7f51\u7edc\u65f6\u89c1\u5230\u8fd9\u4e24\u79cd\u5199\u6cd5\uff0c\u4e00\u79cd\u7528 withContext(Dispatchers.IO)\u8bf7\u6c42\uff0c\u53e6\u4e00\u79cd\u5728 viewscope \u91cc\u8c03\u7528 suspend \u8bf7\u6c42\u7f51\u7edc\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u6709\u4ec0\u4e48\u533a\u522b\uff1f<br \/>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u91cc\u7684 suspend \u65b9\u6cd5\u8fd8\u662f\u5728\u4e3b\u7ebf\u7a0b\u91cc\u6267\u884c\u5417\uff1f      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2606845\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : domosekai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             viewscope \u91cc\u8c03\u7528\u7684 suspend \u51fd\u6570\u5728\u4e0d\u5728\u4e3b\u7ebf\u7a0b\u6267\u884c\uff0c\u8981\u770b\u8fd9\u4e2a\u51fd\u6570\u672c\u8eab\u7684\u5b9e\u73b0\u9009\u62e9\u4e86\u4ec0\u4e48 context \uff08\u5b83\u81ea\u5df1\u4f1a\u8c03\u7528 withcontext \uff09\uff0c\u4e00\u822c\u800c\u8a00\u7f51\u7edc\u76f8\u5173\u7684 API \u5982\u679c\u5c01\u88c5\u6210\u4e86 suspend \u90a3\u4e48\u5c31\u4e0d\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u6267\u884c\uff0c\u4f46\u8fd9\u4e2a\u662f\u5b83\u81ea\u5df1\u9009\u62e9\u7684 context \u800c\u4e0d\u662f\u4f60\u600e\u4e48\u8c03\u7528\u5b83\u6240\u51b3\u5b9a\u7684\u3002\u4f60\u4e5f\u5b8c\u5168\u53ef\u4ee5\u5199\u4e00\u4e2a\u5728\u4e3b\u7ebf\u7a0b\u8dd1\u7684 suspend \u51fd\u6570\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2606846\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : domosekai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u81f3\u4e8e\u4f60\u8bf4\u7684\u524d\u4e00\u79cd\uff0c\u4e00\u822c\u662f\u8981\u8c03\u7528\u7684\u51fd\u6570\u4e0d\u662f suspend \u800c\u662f blocking \u7684\uff0c\u90a3\u4e48\u5c31\u5f97\u7531\u4f60\u7ed9\u4e00\u4e2a\u975e\u4e3b\u7ebf\u7a0b\u7684 context\uff0c\u56e0\u4e3a\u4ed6\u81ea\u5df1\u4e0d\u9009\u62e9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2606847\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mxalbert1996 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             withContext \u662f\u5728\u4e00\u4e2a CoroutineScope \u91cc\u5207\u6362 Context \u7528\u7684\u3002suspend fun \u53ea\u662f\u8868\u793a\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6302\u8d77 Coroutine \u6240\u4ee5\u5fc5\u987b\u5728 CoroutineScope \u91cc\u6216\u8005\u7531\u5176\u4ed6 suspend fun \u8c03\u7528\uff0c\u5b83\u672c\u8eab\u4e0d\u6307\u5b9a Context \u3002\u7528\u9014\u5b8c\u5168\u4e0d\u4e00\u6837\u3002<br \/>\u8bdd\u8bf4\u8fd9\u79cd\u57fa\u672c\u7684\u4e1c\u897f\u8fd8\u662f\u5efa\u8bae\u53bb\u770b\u5b98\u65b9\u6587\u6863\uff0c\u7814\u7a76\u4e00\u4e0b\u6559\u7a0b\u91cc\u7684\u6848\u4f8b\u5c31\u5f88\u5bb9\u6613\u7406\u89e3\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2606848\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : maninfog <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             suspend \u5173\u952e\u5b57\u6807\u8bb0\u51fd\u6570\u8868\u793a\u8be5\u51fd\u6570\u53ea\u80fd\u5728\u534f\u7a0b scope \u4e2d\u6216\u8005\u53e6\u4e00\u4e2a suspend \u65b9\u6cd5\u4e2d\u8c03\u7528\uff0c\u4e00\u822c\u8be5\u51fd\u6570\u4e3a\u8017\u65f6\u51fd\u6570\u3002<br \/>withContext \u5219\u8868\u793a\u5c06\u63a5\u53d7\u7684\u4ee3\u7801\u5757\u5185\u7684\u4e1c\u897f\u653e\u5230\u6307\u5b9a Dispatcher \u4e2d\u8c03\u7528\uff0c\u8c03\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5207\u56de\u5f53\u524d scope \u7ee7\u7eed\u8fd0\u884c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2606849\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fansangg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             withContext \u5207\u7ebf\u7a0b\uff0csuspend \u6807\u8bc6\u4e00\u4e2a\u534f\u7a0b\u51fd\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2606850\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cjh1095358798 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             suspend \u53ea\u662f\u4e00\u4e2a\u6807\u8bb0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>withContext \u4e0e sus&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/138041"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138041"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/138041\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138041"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}