{"id":5178,"date":"2022-03-01T22:45:34","date_gmt":"2022-03-01T14:45:34","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5178"},"modified":"2022-03-02T15:00:02","modified_gmt":"2022-03-02T07:00:02","slug":"%e5%9b%be%e8%a7%a3-swift-async-await","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5178","title":{"rendered":"\u56fe\u89e3 Swift async\/await"},"content":{"rendered":"\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Swift&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Swift<\/a> \u5f00\u53d1\u8005\u7ec8\u4e8e\u5728 WWDC21 \u4e2d\u76fc\u6765\u4e86 async\/await\u3002\u4f5c\u4e3a\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u7684\u6807\u5fd7\u4e4b\u4e00\uff0casync\/await \u53ef\u4ee5\u8ba9\u6211\u4eec\u50cf\u7f16\u5199\u5e38\u89c4\u4ee3\u7801\u4e00\u6837\uff0c\u8f7b\u677e\u5730\u7f16\u5199\u5f02\u6b65\u4ee3\u7801\uff0c\u8fd9\u6837\u80fd\u66f4\u76f4\u89c2\u4e14\u66f4\u5b89\u5168\u5730\u8868\u8fbe\u6211\u4eec\u7684\u601d\u8def\u3002\u540c\u65f6\uff0casync\/await \u662f\u6574\u4e2a Swift \u7ed3\u6784\u5316\u5e76\u53d1\u7684\u57fa\u7840\uff0c\u6240\u4ee5\u6211\u4eec\u4ece\u8fd9\u4e2a Session \u5f00\u59cb\uff0c\u4e00\u8d77\u6765\u63a2\u7d22 Swift \u65b0\u7684\u5e76\u53d1\u6846\u67b6\u3002<\/p>\n\n\n\n<p><strong>\u52a0\u8f7d\u7f29\u7565\u56fe\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1024x462.png\" alt=\"\" class=\"wp-image-5179\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1024x462.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-300x135.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-768x346.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1536x693.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-830x374.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-230x104.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-350x158.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-480x217.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247.png 1658w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u4e2a Session \u901a\u8fc7\u52a0\u8f7d\u7f29\u7565\u56fe\u7247\u4e3a\u6211\u4eec\u6f14\u793a\u4e86 <a href=\"https:\/\/so.csdn.net\/so\/search?q=async&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">async<\/a>\/await \u7684\u4f7f\u7528\u3002\u52a0\u8f7d\u7f29\u7565\u56fe\u7247\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ul><li>\u4ece URL \u5b57\u7b26\u4e32\u521b\u5efa\u4e00\u4e2a URLRequest \u5bf9\u8c61\uff1b<\/li><li>URLSession \u7684 dataTask(with:completion:) \u65b9\u6cd5\u83b7\u53d6\u8981\u8bf7\u6c42\u56fe\u7247\u6570\u636e\uff1b<\/li><li>UIImage(data:) \u4ece\u56fe\u7247\u6570\u636e\u4e2d\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\uff1b<\/li><li>UIImage \u7684 prepareThumbnail \u65b9\u6cd5\u4ece\u539f\u59cb\u56fe\u50cf\u4e2d\u6e32\u67d3\u4e00\u4e2a\u7f29\u7565\u56fe<\/li><\/ul>\n\n\n\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u7684\u6bcf\u4e00\u6b65\u90fd\u4f9d\u8d56\u4e8e\u524d\u4e00\u4e2a\u6b65\u9aa4\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u5fc5\u987b\u6309\u987a\u5e8f\u6267\u884c\u3002\u5728\u8fd9\u56db\u6b65\u64cd\u4f5c\u4e2d\uff0c\u7b2c\u4e8c\u6b65\u548c\u7b2c\u56db\u6b65\u4f1a\u6bd4\u8f83\u8d39\u65f6\uff0c\u6240\u4ee5\u8fd9\u4e24\u6b65\u64cd\u4f5c\u4e00\u822c\u901a\u8fc7\u5f02\u6b65\u8c03\u7528\u6765\u5b8c\u6210\u3002<\/p>\n\n\n\n<p><strong>\u539f\u59cb\u7248\u672c\u4ee3\u7801<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-1024x515.png\" alt=\"\" class=\"wp-image-5180\" width=\"668\" height=\"336\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-1024x515.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-300x151.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-768x386.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-1536x773.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-830x417.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-230x116.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-350x176.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1-480x241.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-1.png 1674w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/figure><\/div>\n\n\n\n<p>\u539f\u59cb\u7684\u8c03\u7528\u65b9\u5f0f\u5982\u4e0a\u4ee3\u7801\u6240\u793a\uff0c\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a fetchThumbnail \u51fd\u6570\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a completion \u53c2\u6570\uff0c\u7528\u4e8e\u5c06\u8f93\u51fa\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n\n\n\n<p>fetchThumbnail \u51fd\u6570\u4f53\u4e2d\u89c4\u4e2d\u4e3e\u5730\u6309\u4ee5\u4e0a\u6b65\u9aa4\u6765\u5b8c\u6210\u4efb\u52a1<\/p>\n\n\n\n<ul><li>\u8c03\u7528 thumbnailURLRequest\uff0c\u8be5\u64cd\u4f5c\u662f\u540c\u6b65\u7684\uff1b<\/li><li>\u8c03\u7528 URLSession \u7684 dataTask(with:completion:)\uff0c\u8fd9\u662f\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u9700\u8981\u8c03\u7528 resume() \u65b9\u6cd5\u542f\u52a8\u5f02\u6b65\u5de5\u4f5c\uff1bfetchThumbnail \u8fd4\u56de\u6267\u884c\u5176\u5b83\u4efb\u52a1<\/li><li>\u5728 dataTask(with:completion:) \u7684\u5b8c\u6210\u5904\u7406\u7a0b\u5e8f\u4e2d\uff0c\u5904\u7406\u83b7\u53d6\u6570\u636e\u540e\u7684\u64cd\u4f5c\uff1b<\/li><li>\u6b63\u5e38\u83b7\u53d6\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7 UIImage(data:) \u6765\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u662f\u540c\u6b65\u7684\uff1b<\/li><li>\u8c03\u7528 UIImage \u5bf9\u8c61\u7684 prepareThumbnail \u65b9\u6cd5\u751f\u6210\u7f29\u7565\u56fe\uff0c\u8be5\u65b9\u6cd5\u4e5f\u662f\u4e2a\u5f02\u6b65\u65b9\u6cd5\uff1b<\/li><\/ul>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u660e\u663e\u7684\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul><li>\u5fc5\u987b\u65f6\u523b\u6ce8\u610f\u5728\u54ea\u91cc\u9700\u8981\u8c03\u7528 completion\uff08\u8fd9\u91cc\u5c31\u6709 5 \u5904\uff09\uff0c\u5982\u679c\u5fd8\u8bb0\u8c03\u7528 completion \u6765\u901a\u77e5\u8c03\u7528\u8005\u5931\u8d25\uff08Swift \u4e0d\u80fd\u4fdd\u8bc1\u5f3a\u5236\u6267\u884c completion\uff09\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u6d41\u7a0b\u5f02\u5e38\uff1b<\/li><li>\u8fd9\u91cc\u5927\u7ea6 20 \u884c\u7684\u4ee3\u7801\uff0c\u770b\u4f3c\u662f\u6309\u6d41\u7a0b\u6765\u8d70\uff0c\u4f46\u5c42\u5c42\u5d4c\u5957\u4f1a\u8ba9\u4ee3\u7801\u663e\u793a\u66f4\u52a0\u6666\u6da9\uff08\u6240\u8c13\u7684\u56de\u8c03\u5730\u72f1\uff09\uff1b<\/li><\/ul>\n\n\n\n<p>\u6709\u4e00\u4e9b\u73b0\u6210\u7684\u6539\u8fdb\u65b9\u6848\uff0c\u4f46\u6548\u679c\u5e76\u4e0d\u7406\u60f3\uff1a<\/p>\n\n\n\n<ul><li>completion \u7684\u5165\u53c2\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u7684 Result\uff0c\u4f1a\u66f4\u5b89\u5168\u4e00\u70b9\uff0c\u4f46\u4e5f\u53ea\u662f\u4e00\u70b9\u70b9\uff1b<\/li><li>\u7c7b future \u65b9\u6cd5\uff0c\u4f46\u4ee3\u7801\u6ca1\u6709\u66f4\u7b80\u6d01\u548c\u5b89\u5168<\/li><\/ul>\n\n\n\n<p>\u73b0\u5728\uff0c\u5fc5\u987b\u796d\u51fa async\/await \u4e86\u3002<\/p>\n\n\n\n<p><strong>async\/await \u7248\u672c\u4ee3\u7801<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-1024x342.png\" alt=\"\" class=\"wp-image-5182\" width=\"678\" height=\"226\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-1024x342.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-300x100.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-768x256.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-830x277.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-230x77.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-350x117.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2-480x160.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-2.png 1372w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/figure><\/div>\n\n\n\n<p>async\/await \u7248\u672c\u4ee3\u7801\u5982\u4e0a\u6240\u793a\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u7b7e\u540d\u6709\u51e0\u5904\u660e\u663e\u7684\u53d8\u5316\uff1a<\/p>\n\n\n\n<ul><li>\u5165\u53c2\u4e0d\u518d\u7684 completion \u5904\u7406\u7a0b\u5e8f\uff1b<\/li><li>\u8fd4\u56de\u503c\u662f UIImage \u7c7b\u578b\uff0c\u8868\u793a\u51fd\u6570\u8fd4\u56de\u7684\u7f29\u7565\u56fe\uff0c\u540c\u65f6\u901a\u8fc7\u5173\u952e\u5b57 throws \u6807\u8bc6\u53ef\u4ee5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff1b<\/li><li>\u5728 throws \u524d\u9762\u6dfb\u52a0\u4e86\u5173\u952e\u5b57 async\uff0c\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff08\u6ce8\u610f\uff1a\u5982\u679c\u6ca1\u6709 throws \u5173\u952e\u5b57\uff0c\u5219 async \u76f4\u63a5\u653e\u5728 -&gt; \u524d\u9762\uff09<\/li><\/ul>\n\n\n\n<p>fetchThumbnail \u7684\u5b9e\u73b0\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\uff1a<\/p>\n\n\n\n<ul><li>\u8c03\u7528\u4f9d\u7136\u4ece thumbnailURLRequest \u5f00\u59cb\uff0c\u8be5\u64cd\u4f5c\u662f\u540c\u6b65\u7684\uff0c\u963b\u585e\u7ebf\u7a0b\uff1b<\/li><li>\u8c03\u7528 URLSession \u7684 data(for:) \u5f00\u59cb\u4e0b\u8f7d\u6570\u636e\uff0c\u8fd9\u91cc\u6709\u51e0\u4e2a\u53d8\u5316<\/li><\/ul>\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=\"\">   \u2022 \u4f7f\u7528 await \u6807\u8bb0\u65b9\u6cd5\u8c03\u7528\uff0c\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff1b\u5982\u679c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u91cc\u9762\u6709\u591a\u4e2a\u5f02\u6b65\u51fd\u6570\u8c03\u7528\uff0c\u5219\u53ea\u9700\u8981\u5199\u4e00\u6b21 await\n   \u2022 data(for: ) \u65b9\u6cd5\u662f\u53ef\u7b49\u5f85\u7684\uff0c\u8c03\u7528\u540e\uff0c\u4f1a\u6302\u8d77\u81ea\u5df1\uff0c\u89e3\u9664\u7ebf\u7a0b\u963b\u585e\uff1b\n   \u2022 \u4f7f\u7528 try \u662f\u56e0\u4e3a fetchThumbnail \u88ab\u6807\u8bb0\u4e3a throws\uff0c\u5982\u679c\u7f51\u7edc\u8bf7\u6c42\u6709\u5f02\u6b65\uff0c\u5219\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff1b\n   \u2022 data(for:) \u5b8c\u6210\u540e\uff0c\u6062\u590d fetchThumbnail\uff0c\u5e76\u5c06\u8fd4\u56de\u7684\u6570\u636e\u53ca\u8bf7\u6c42\u54cd\u5e94\u8d4b\u503c\u7ed9 data \u548c response\uff0c\u5c31\u50cf\u666e\u901a\u7684\u8d4b\u503c\u64cd\u4f5c\u4e00\u6837\uff1b<\/pre>\n\n\n\n<ul><li>\u5224\u65ad\u54cd\u5e94\u662f\u5426\u6709\u6548\uff0c\u5982\u679c\u54cd\u5e94\u7801\u4e0d\u4e3a 200\uff0c\u5219\u629b\u51fa\u5f02\u6b65\uff0c\u8fd9\u4e00\u6b65\u4e3a\u540c\u6b65\u64cd\u4f5c\uff1b<\/li><li>\u6b63\u5e38\u83b7\u53d6\u6570\u636e\u540e\uff0c\u901a\u8fc7 UIImage(data:) \u6765\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u662f\u540c\u6b65\u7684\uff1b<\/li><li>\u8c03\u7528 UIImage \u5bf9\u8c61\u7684 thumbnail \u5c5e\u6027\u751f\u6210\u7f29\u7565\u56fe\uff0c\u8fd9\u662f\u4e2a<strong>\u53ef\u7b49\u5f85\u7684\u5c5e\u6027<\/strong>\uff08\u6b64\u5904\u4e3a\u975e SDK \u5185\u7f6e\u7684\u5c5e\u6027\uff0c\u800c\u662f\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\uff09\uff0c\u8fd9\u91cc\u540c\u6837\u4f7f\u7528 await \u6765\u6807\u8bc6\u5f02\u6b65\u64cd\u4f5c\uff1b\u5982\u679c thumbnail \u5931\u8d25\uff0c\u5219\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff1b<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u77ed\u77ed\u7684 6 \u884c\u4ee3\u7801\u5c31\u5b9e\u73b0\u4e86\u4e0a\u9762\u5927\u7ea6 20 \u884c\u4ee3\u7801\u7684\u529f\u80fd\uff0c\u4f18\u70b9\u663e\u800c\u6613\u89c1\uff1a<\/p>\n\n\n\n<ul><li>\u66f4\u5b89\u5168\uff1a\u6574\u4e2a\u8fc7\u7a0b\u80fd\u786e\u4fdd\u51fa\u9519\u65f6\u629b\u51fa\u5f02\u5e38\uff1b<\/li><li>\u66f4\u7b80\u6d01\uff1a\u907f\u514d\u7684\u4ee3\u7801\u7684\u5c42\u5c42\u5d4c\u5957\uff1b<\/li><li>\u66f4\u80fd\u4f53\u73b0\u610f\u56fe\uff1a\u6574\u4e2a\u4ee3\u7801\u57fa\u672c\u662f\u548c\u6211\u4eec\u9884\u5b9a\u7684\u6d41\u7a0b\u4fdd\u6301\u4e86\u4e00\u81f4\uff1b<\/li><\/ul>\n\n\n\n<p><strong>\u53ef\u7b49\u5f85\u5c5e\u6027<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-1024x563.png\" alt=\"\" class=\"wp-image-5186\" width=\"625\" height=\"343\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-1024x563.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-300x165.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-768x423.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-830x457.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-230x127.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-350x193.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3-480x264.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-3.png 1054w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u4e0a\u4ee3\u7801\uff0c\u662f\u4e0a\u9762\u4f7f\u7528\u7684 thumbnail \u5c5e\u6027\u7684\u5b9e\u73b0\u3002\u5b83\u662f UIImage \u7684\u4e00\u4e2a\u6269\u5c55\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u5f02\u6b65\u5c5e\u6027\u5fc5\u987b\u662f\u53ea\u8bfb\u7684\uff0c\u53ef\u5199\u5c5e\u6027\u4e0d\u80fd\u58f0\u660e\u4e3a\u5f02\u6b65\u5c5e\u6027\uff1b<\/li><li>\u5f02\u6b65\u5c5e\u6027\u9700\u8981\u6709\u4e00\u4e2a\u660e\u786e\u7684 getter\uff0casync \u5173\u952e\u5b57\u4f4d\u4e8e get \u540e\uff1b<\/li><li>\u4ece Swift 5.5 \u5f00\u59cb\uff0cgetter \u4e5f\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\uff0c\u5982\u679c\u540c\u65f6\u662f\u5f02\u6b65\u7684\uff0c\u5219 async \u5173\u952e\u5b57\u4f4d\u4e8e throws \u524d\u9762\uff1b<\/li><li>await \u53ef\u7528\u4e8e\u5c5e\u6027 body \u4e2d\u7684\u8868\u8fbe\u5f0f\uff0c\u4ee5\u8868\u660e\u64cd\u4f5c\u7684\u5f02\u6b65\u6027\uff1b<\/li><\/ul>\n\n\n\n<p><strong>\u8c03\u7528\u6d41\u7a0b\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-1024x571.png\" alt=\"\" class=\"wp-image-5187\" width=\"605\" height=\"337\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-1024x571.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-300x167.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-768x428.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-1536x856.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-830x463.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-230x128.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-350x195.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4-480x267.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-4.png 1938w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/figure><\/div>\n\n\n\n<p>\u666e\u901a\u51fd\u6570\u7684\u8c03\u7528\u6d41\u7a0b\u5982\u4e0a\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<ul><li>\u8c03\u7528\u51fd\u6570\uff1b<\/li><li>\u51fd\u6570\u83b7\u53d6\u7ebf\u7a0b\u7684\u63a7\u5236\u6743\uff0c\u5e76\u5b8c\u5168\u5360\u7528\u8be5\u7ebf\u7a0b\uff1b<\/li><li>\u51fd\u6570\u6267\u884c\u5b8c\u6210\u8fd4\u56de\u6216\u8005\u629b\u51fa\u9519\u8bef\uff0c\u5c06\u63a7\u5236\u6743\u4ea4\u8fd8\u8c03\u7528\u65b9<\/li><\/ul>\n\n\n\n<p>\u8fd9\u91cc\u666e\u901a\u51fd\u6570\u653e\u5f03\u7ebf\u7a0b\u63a7\u5236\u6743\u7684\u552f\u4e00\u65b9\u5f0f\u5c31\u662f<strong>\u6267\u884c\u5b8c\u6210<\/strong>\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-1024x491.png\" alt=\"\" class=\"wp-image-5188\" width=\"623\" height=\"298\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-1024x491.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-300x144.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-768x368.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-1536x736.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-2048x981.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-830x398.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-230x110.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-350x168.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-5-480x230.png 480w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/figure><\/div>\n\n\n\n<p>\u800c\u5f02\u6b65\u51fd\u6570\u7684\u8c03\u7528\u6d41\u7a0b\u5219\u5982\u4e0a\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<ul><li>\u8c03\u7528\u51fd\u6570\uff1b<\/li><li>\u51fd\u6570\u83b7\u5f97\u7ebf\u7a0b\u63a7\u5236\u6743\uff1b<\/li><li>\u51fd\u6570\u8fd0\u884c\u540e\uff0c\u6302\u8d77\uff0c\u540c\u65f6\u653e\u5f03\u5bf9\u7ebf\u7a0b\u7684\u63a7\u5236\uff0c\u5e76\u5c06\u63a7\u5236\u6743\u4ea4\u7ed9\u7cfb\u7edf\uff0c\u7cfb\u7edf\u53ef\u81ea\u7531\u652f\u914d\u8be5\u7ebf\u7a0b\uff1b<\/li><li>\u7cfb\u7edf\u786e\u5b9a\u4f55\u65f6\u6062\u590d\u51fd\u6570\uff1b<\/li><li>\u51fd\u6570\u6062\u590d\u540e\u91cd\u65b0\u83b7\u5f97\u63a7\u5236\u6743\uff0c\u5e76\u7ee7\u7eed\u5de5\u4f5c\uff1b<\/li><li>\u51fd\u6570\u6267\u884c\u5b8c\u6210\u6216\u629b\u51fa\u5f02\u5e38\u540e\uff0c\u8fd4\u56de\u8c03\u7528\u65b9\uff0c\u5c06\u63a7\u5236\u6743\u4ea4\u8fd8\u7ed9\u8c03\u7528\u65b9<\/li><\/ul>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u6302\u8d77\u65f6\uff0c\u4e5f\u4f1a\u6302\u8d77\u5b83\u7684\u8c03\u7528\u8005\uff0c\u6240\u4ee5\u8c03\u7528\u8005\u4e5f\u5fc5\u987b\u662f\u5f02\u6b65\u7684\uff1b<\/li><li>\u5f02\u6b65\u51fd\u6570\u53ef\u4ee5\u591a\u6b21\u6302\u8d77\uff0c\u5c31\u50cf\u4e0a\u9762\u7684 fetchThumbnail \u65b9\u6cd5\u4e00\u6837\u4f7f\u7528\u4e86\u4e24\u4e2a await \u5173\u952e\u5b57\uff1b<\/li><li>\u5f02\u6b65\u51fd\u6570\u6302\u8d77\u65f6\uff0c\u4e0d\u4f1a\u963b\u585e\u7ebf\u7a0b\uff1b<\/li><li>\u5f02\u6b65\u51fd\u6570\u53ef\u80fd\u4f1a\u5728\u4e00\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e0a\u6062\u590d\uff1b<\/li><li>async \u51fd\u6570\u5e76\u4e0d\u4e00\u5b9a\u4f1a\u6302\u8d77\uff1b<\/li><\/ul>\n\n\n\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-1024x609.png\" alt=\"\" class=\"wp-image-5190\" width=\"595\" height=\"353\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-1024x609.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-300x178.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-768x457.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-1536x913.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-830x493.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6-480x285.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-6.png 1736w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ee5\u5f80\uff0c\u6211\u4eec\u60f3\u8981\u5bf9\u7f51\u7edc\u8bf7\u6c42\u4efb\u52a1\u505a\u4e00\u4e9b\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u9700\u8981\u501f\u52a9 XCTestExpectation \u8fd9\u4e2a\u7c7b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-1024x600.png\" alt=\"\" class=\"wp-image-5191\" width=\"611\" height=\"358\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-1024x600.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-300x176.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-768x450.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-1536x900.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-830x486.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-230x135.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-350x205.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7-480x281.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-7.png 1738w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/figure><\/div>\n\n\n\n<p>\u800c\u6709\u4e86 async\/await \u540e\uff0c\u5f02\u6b65\u51fd\u6570\u7684\u5355\u5143\u6d4b\u8bd5\u66f4\u52a0\u7b80\u5355\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u5f02\u6b65\u4efb\u52a1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-1024x513.png\" alt=\"\" class=\"wp-image-5192\" width=\"649\" height=\"324\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-1024x513.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-300x150.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-768x385.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-1536x770.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-830x416.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-230x115.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-350x175.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8-480x241.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-8.png 1796w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u4eec\u4e0a\u9762\u63d0\u5230\uff0c\u5f02\u6b65\u51fd\u6570\u7684\u8c03\u7528\u8005\u4e5f\u9700\u8981\u662f\u5f02\u6b65\u51fd\u6570\u3002\u4f46\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u786e\u5b9e\u9700\u8981\u5728\u540c\u6b65\u65b9\u6cd5\u4e2d\u8c03\u7528\u5f02\u6b65\u51fd\u6570\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u7528\u5230\u5f02\u6b65\u4efb\u52a1\u529f\u80fd\u4e86\u3002<\/p>\n\n\n\n<p>\u5373\u5982\u4e0a\u4ee3\u7801\uff0c\u901a\u8fc7 async \u95ed\u5305\u5c06\u4efb\u52a1\u6253\u5305\uff0c\u4ee5\u6267\u884c\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u4ee3\u7801\u8fc1\u79fb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5bf9\u4e8e\u8001\u4ee3\u7801\uff0c\u6211\u4eec\u66f4\u5173\u6ce8\u7684\u662f\u5982\u4f55\u66f4\u597d\u5730\u4f7f\u7528\u4e0a\u65b0\u7279\u6027\u3002\u5bf9\u4e8e async\/await\uff0cSwift \u56e2\u961f\u4e5f\u4e3a\u6211\u4eec\u505a\u4e86\u5f88\u591a\u51c6\u5907\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-1024x427.png\" alt=\"\" class=\"wp-image-5193\" width=\"612\" height=\"255\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-1024x427.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-300x125.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-768x320.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-1536x641.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-830x346.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-230x96.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-350x146.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9-480x200.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-9.png 1898w\" sizes=\"(max-width: 612px) 100vw, 612px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-1024x595.png\" alt=\"\" class=\"wp-image-5194\" width=\"615\" height=\"357\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-1024x595.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-300x174.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-768x446.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-830x482.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-230x134.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-350x203.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10-480x279.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-10.png 1312w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/figure><\/div>\n\n\n\n<p>\u2022 \u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u5f02\u6b65 API\uff0c\u4ee5\u66ff\u4ee3\u91c7\u7528\u5b8c\u6210\u5904\u7406\u7a0b\u5e8f\u7684 API<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-1024x633.png\" alt=\"\" class=\"wp-image-5196\" width=\"595\" height=\"368\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-1024x633.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-300x185.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-768x474.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-1536x949.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-830x513.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-230x142.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-350x216.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11-480x297.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-11.png 1554w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n\n<ul><li>\u8bb8\u591a\u59d4\u6258\u65b9\u6cd5\u4e5f\u6709\u76f8\u5e94\u7684\u5f02\u6b65\u66ff\u4ee3\u65b9\u6cd5<\/li><\/ul>\n\n\n\n<p><strong>Continuation \u6a21\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-1024x538.png\" alt=\"\" class=\"wp-image-5198\" width=\"592\" height=\"311\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-1024x538.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-300x157.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-768x403.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-1536x806.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-830x436.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-350x184.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12-480x252.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-12.png 1962w\" sizes=\"(max-width: 592px) 100vw, 592px\" \/><\/figure><\/div>\n\n\n\n<p>Swift \u662f\u5982\u4f55\u4e0e\u7cfb\u7edf\u534f\u4f5c\uff0c\u5b8c\u6210\u5f02\u6b65\u4ee3\u7801\u7684\u6062\u590d\u5462\u3002\u7b54\u6848\u5c31\u662f Continuation \u6a21\u5f0f\uff0c\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u7b49\u5f85\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u95ed\u5305\u6765\u6307\u5b9a\u4e0b\u4e00\u6b65\u8981\u505a\u4ec0\u4e48\u3002\u5f53\u51fd\u6570\u8c03\u7528\u5b8c\u6210\u65f6\uff0c\u8c03\u7528\u5b8c\u6210\u5904\u7406\u7a0b\u5e8f\u6062\u590d\u8c03\u7528\u8005\u60f3\u8981\u5bf9\u7ed3\u679c\u6267\u884c\u7684\u64cd\u4f5c\u3002\u8fd9\u79cd\u534f\u540c\u6267\u884c\u6b63\u662f Swift \u4e2d\u5f02\u6b65\u51fd\u6570\u7684\u5de5\u4f5c\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e4b\u524d\u4f7f\u7528\u7684<code>completion handler<\/code>\u65b9\u5f0f\u7684\u65b9\u6cd5\uff0c\u6216\u8005\u7b2c\u4e09\u65b9\u7684\u5e93\u4e2d\u4f7f\u7528\u7684completion \u65b9\u5f0f\u3002\u9700\u8981\u8fdb\u884c\u5305\u88c5\u540e\u4f7f\u7528\u3002swift \u63d0\u4f9b\u4e86 <code>withUnsafeContinuation<\/code> \u3001<code>withCheckedThrowingContinuation<\/code>\u3001 <code>withCheckedContinuation<\/code> \u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\uff0cSwift \u63d0\u4f9b\u4e86 withCheckedThrowingContinuation \u51fd\u6570<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Suspends the current task, then calls the given closure with a checked throwing continuation for the current task.<\/p><\/blockquote>\n\n\n\n<p>\u4ee5\u53ca CheckedContinuation \u7ed3\u6784\u4f53<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>A mechanism to interface between synchronous and asynchronous code, logging correctness violations.<\/p><\/blockquote>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u7ed3\u6784\u4f53\u548c\u51fd\u6570\uff0c\u8c03\u7528\u65b9\u53ef\u4ee5\u8bbf\u95ee\u53ef\u7528\u4e8e\u6062\u590d\u6302\u8d77\u7684\u5f02\u6b65\u51fd\u6570\u7684\u5ef6\u7eed\u503c\u3002CheckedContinuation \u7ed3\u6784\u4f53\u8fd8\u63d0\u4f9b\u4e86\u591a\u4e2a resume \u65b9\u6cd5\uff0c\u7528\u6765\u56de\u4f20\u7ed3\u679c\u503c\u3002<\/p>\n\n\n\n<p>Continuation \u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u624b\u52a8\u63a7\u5236\u5f02\u6b65\u51fd\u6570\u7684\u6267\u884c\uff0c\u4e0d\u8fc7\u6709\u4e00\u70b9\u9700\u8981\u8bb0\u4f4f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>resume \u5728\u6bcf\u4e2a\u4ee3\u7801\u5206\u652f\u4e0a\u5fc5\u987b\u4e14\u53ea\u80fd\u8c03\u7528\u4e00\u6b21<\/p><\/blockquote>\n\n\n\n<p>public func resume(returning x: T) \u63a5\u6536 <code>completion<\/code> \u4e2d\u7684\u6570\u636e\u8fd4\u56de\uff0c\u8f6c\u6362\u6210<code>async<\/code> \u51fd\u6570\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>public func resume(throwing x: E) \u8fdb\u884c\u629b\u51fa\u5f02\u5e38<br><code>withCheckedContinuation<\/code> \u65b9\u6cd5\u4e2d\u7684<code>checked<\/code> \u4f1a\u5728\u8fd0\u884c\u65f6\u5bf9\u64cd\u4f5c\u8fdb\u884c\u68c0\u67e5\uff1a\u662f\u5426\u8c03\u7528<code>resume<\/code>\u8fdb\u884c\u8fd4\u56de\u3002\u5982\u679c\u4e0d\u8c03\u7528\u4f1a\u9020\u6210\u8d44\u6e90\u6cc4\u9732\u3002\u591a\u6b21\u8c03\u7528\u4e5f\u4f1a\u9020\u6210\u95ee\u9898\u3002<br><strong>continuation \u6709\u4e14\u53ea\u80fd resume \u4e00\u6b21\u3002<\/strong><\/p>\n\n\n\n<p><code>withUnsafeContinuation<\/code> \u7684\u5de5\u4f5c\u673a\u5236\u548c<code>withCheckedContinuation<\/code> \u4e00\u81f4\uff0c\u552f\u4e00\u533a\u522b\u5728\u4e8e\u8fd0\u884c\u65f6\u4e0d\u4f1a\u5bf9\u64cd\u4f5c\u8fdb\u884c\u68c0\u67e5\u3002\u4f46\u6027\u80fd\u66f4\u597d\u3002\u5b9e\u9645\u4f7f\u7528\u4e2d<code>withCheckedContinuation<\/code>\u6d4b\u8bd5\u6ca1\u6709\u95ee\u9898\u540e\uff0c\u6b63\u5f0f\u53d1\u5e03\u65f6\u4f7f\u7528<code>withUnsafeContinuation<\/code><\/p>\n\n\n\n<p>withCheckedContinuation:<\/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=\"\">func sendCodeContinuation() async -> Data{\n    \n    await withCheckedContinuation{ continuation in\n        NET.GET(url: \"\").success { data in\n            continuation.resume(returning: data)\n        }\n    } as! Data\n}\n<\/pre>\n\n\n\n<p>\u5982\u679c\u6709\u629b\u51fa\u5f02\u5e38<br>withCheckedThrowingContinuation:<\/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=\"\">func sendCodeThrowsContinuation() async throws -> Data{\n    \n    try await withCheckedThrowingContinuation { (continuation:CheckedContinuation&lt;Data,Error>) in\n       NET.POST(url: \"\").success { data in\n           continuation.resume(returning: data as! Data)\n\n       }.failed { error in\n           continuation.resume(throwing: error as! Error)\n       }\n    }<\/pre>\n\n\n\n<p>\u5982\u679c\u67d0\u4e2a\u5206\u652f\u6ca1\u6709\u8c03\u7528 resume\uff0c\u5f02\u6b65\u8c03\u7528\u5c06\u6c38\u8fdc\u6302\u8d77\uff1b\u800c\u5982\u679c\u67d0\u4e2a\u5206\u652f\u8c03\u7528\u4e86\u591a\u6b21\uff0c\u5219\u53ef\u80fd\u4f1a\u7834\u574f\u7a0b\u5e8f\u6570\u636e\u3002\u8fd9\u4e24\u79cd\u60c5\u51b5 Swift \u90fd\u4f1a\u7ed9\u51fa\u8b66\u544a\u6216\u9519\u8bef\u3002<\/p>\n\n\n\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u57285.5\u4e4b\u524d\u8fdb\u884c\u5f02\u6b65\u64cd\u4f5c\uff0c\u8c03\u7528\u8fd4\u56de\u65f6\uff0c\u4f7f\u7528<code>completion handler<\/code>\u53c2\u6570\u8fdb\u884c\u5904\u7406\u3002\u73b0\u5728\u63d0\u4f9b\u4e86<strong>async\/awiat<\/strong> \u8fdb\u884c\u5f02\u6b65\u5e76\u53d1\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u57fa\u672c\u4f7f\u7528\u65b9\u5f0f\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=\"\">func sendCode() async throws -> Data{\n\n    let request = URLRequest.init(url: URL(string: \"\")!)\n    let (data, response) = try await URLSession.shared.data(for: request)\n    \n    guard (response as? HTTPURLResponse)?.statusCode == 200 else {\n        throw RequestError.notdata\n    }\n    \n    return data      \n}<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u540e\u9762\u8ddf\u4e0a<code>async<\/code> \u8868\u793a\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u3002\u5982\u679c\u8c03\u7528\u5f02\u6b65\u51fd\u6570<\/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=\"\">func groupTaskLoadData() async throws {   \n    do{\n        try await sendCode()\n    }catch let error as RequestError{\n       if error == RequestError.urlerror {\n           print(\"xxxx\")\n        }\n    }       \n}<\/pre>\n\n\n\n<p>\u5728\u6b63\u5e38\u8fd4\u56de\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 async \u4fee\u9970\u7684func\u65f6\uff0c\u9700\u8981\u7528Task{} \u8fdb\u884c\u5305\u88c5\uff0c\u5426\u5219\u62a5\u9519\u3002\u4e5f\u5c31\u662f\u8bf4\u5f02\u6b65\u51fd\u6570\u53ea\u80fd\u5728\u5f02\u6b65\u51fd\u6570\u4e2d\u8c03\u7528\uff0c\u5982\u679c\u6b63\u5e38\u51fd\u6570\u8c03\u7528\u7684\u8bdd\uff0c\u9700\u8981\u7528Task{} \u8fdb\u884c\u5305\u88c5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Cannot pass function of type &#8216;() async -> Void&#8217; to parameter expecting synchronous function type<\/p><\/blockquote>\n\n\n\n<p>\u4f7f\u7528\u65b9\u5f0f\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=\"\">Button(\"Test Async\") {\n     \n  Task{ \n     await sendCode()   \n    }\n}<\/pre>\n\n\n\n<p>\u5c5e\u6027\u4e5f\u53ef\u4ee5 <code>async properties<\/code>\uff08\u4f7f\u7528\u5f02\u6b65\u5c5e\u6027\uff0c\u5fc5\u987b\u53ea\u80fd\u662f<code>get<\/code>\u5c5e\u6027\u3002\u53ef\u5199\u5c5e\u6027\u4e0d\u80fd\u4f7f\u7528\u5f02\u6b65\u5c5e\u6027\u3002\uff09<\/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=\"\">var isAuth: Bool {\n    get async {\n        await self.sendCode()\n    }\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u4e0b\u9762\u7684\u4f8b\u5b50\uff0cprint(&#8220;AAAA&#8221;)\u548c print(&#8220;BBBB&#8221;)\u5148\u6267\u884c\u4e86\uff0c\u7136\u540e\u624d\u6267\u884c\u7684\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-1024x742.png\" alt=\"\" class=\"wp-image-5203\" width=\"620\" height=\"448\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-1024x742.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-300x218.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-230x167.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-350x254.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/03\/\u56fe\u7247-13-480x348.png 480w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u5bf9\u300aMeet async\/await in Swift\u300b\u7684\u6574\u7406\uff0c\u5c55\u793a\u4e86 async\/await \u7684\u4f7f\u7528\u53ca\u8fd0\u884c\u673a\u5236\uff0c\u6b22\u8fce\u6765\u5230 Swift \u65b0\u4e16\u754c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift \u5f00\u53d1\u8005\u7ec8\u4e8e\u5728 WWDC21 \u4e2d\u76fc\u6765\u4e86 async\/await\u3002\u4f5c\u4e3a\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u7684\u6807\u5fd7\u4e4b\u4e00\uff0casyn [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5178"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5178"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5178\/revisions"}],"predecessor-version":[{"id":5205,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5178\/revisions\/5205"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}