{"id":1188,"date":"2020-11-06T00:37:53","date_gmt":"2020-11-05T16:37:53","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1188"},"modified":"2020-11-06T00:37:53","modified_gmt":"2020-11-05T16:37:53","slug":"swift-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%9a%84%e5%87%a0%e4%b8%aa%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1188","title":{"rendered":"Swift \u591a\u7ebf\u7a0b\u7684\u51e0\u4e2a\u4f8b\u5b50"},"content":{"rendered":"\n<ul><li><strong>\u6dfb\u52a0\u4efb\u52a1\u5230\u961f\u5217\u7684\u65b9\u6cd5<\/strong>\u00a0<\/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=\"\">DispatchQueue.global(qos: .default).async {\n    \/\/\u5904\u7406\u8017\u65f6\u64cd\u4f5c\u7684\u4ee3\u7801\u5757...\n    print(\"do work\")\n     \n    \/\/\u64cd\u4f5c\u5b8c\u6210\uff0c\u8c03\u7528\u4e3b\u7ebf\u7a0b\u6765\u5237\u65b0\u754c\u9762\n    DispatchQueue.main.async {\n        print(\"main refresh\")\n    }\n}\n\n\/\/\u6dfb\u52a0\u540c\u6b65\u4ee3\u7801\u5757\u5230global\u961f\u5217\n\/\/\u4e0d\u4f1a\u9020\u6210\u6b7b\u9501\uff0c\u4f46\u4f1a\u4e00\u76f4\u7b49\u5f85\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6bd5\nDispatchQueue.global(qos: .default).sync {\n    print(\"sync1\")\n}\nprint(\"end1\")\n\n\n\/\/\u6dfb\u52a0\u540c\u6b65\u4ee3\u7801\u5757\u5230main\u961f\u5217\n\/\/\u4f1a\u5f15\u8d77\u6b7b\u9501\n\/\/\u56e0\u4e3a\u5728\u4e3b\u7ebf\u7a0b\u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a\u4efb\u52a1\uff0c\u56e0\u4e3a\u662f\u540c\u6b65\uff0c\u6240\u4ee5\u8981\u7b49\u6dfb\u52a0\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\u624d\u80fd\u7ee7\u7eed\u8d70\u4e0b\u53bb\u3002\u4f46\u662f\u65b0\u6dfb\u52a0\u7684\u4efb\u52a1\u6392\u5728\n\/\/\u961f\u5217\u7684\u672b\u5c3e\uff0c\u8981\u6267\u884c\u5b8c\u6210\u5fc5\u987b\u7b49\u524d\u9762\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u7531\u6b64\u53c8\u56de\u5230\u4e86\u7b2c\u4e00\u6b65\uff0c\u7a0b\u5e8f\u5361\u6b7b\nDispatchQueue.main.sync {\n    print(\"sync2\")\n}\nprint(\"end2\")\n<\/pre>\n\n\n\n<ul><li><strong>\u53ea\u6267\u884c\u4e00\u6b21<\/strong><\/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=\"\">\/\/\u5f80dispatch_get_global_queue\u961f\u5217\u4e2d\u6dfb\u52a0\u4ee3\u7801\u5757\uff0c\u53ea\u6267\u884c\u4e00\u6b21\nvar predicate:dispatch_once_t = 0\ndispatch_once(&amp;predicate, { () -> Void in\n    \/\/\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u53ef\u7528\u4e8e\u521b\u5efa\u5355\u4f8b\n    println(\"work\")\n})\n<\/pre>\n\n\n\n<ul><li><strong>asyncAfter\u00a0\u5ef6\u8fdf\u8c03\u7528<\/strong><\/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=\"\">\/\/\u5ef6\u65f62\u79d2\u6267\u884c\nDispatchQueue.global(qos: .default).asyncAfter(deadline: DispatchTime.now() + 2.0) {\n    print(\"after!\")\n}\n<\/pre>\n\n\n\n<ul><li><strong>\u4fe1\u53f7\uff0c\u4fe1\u53f7\u91cf<\/strong><\/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=\"\">DispatchSemaphore(value: )\uff1a\u7528\u4e8e\u521b\u5efa\u4fe1\u53f7\u91cf\uff0c\u53ef\u4ee5\u6307\u5b9a\u521d\u59cb\u5316\u4fe1\u53f7\u91cf\u8ba1\u6570\u503c\uff0c\u8fd9\u91cc\u6211\u4eec\u9ed8\u8ba41.\nsemaphore.wait()\uff1a\u4f1a\u5224\u65ad\u4fe1\u53f7\u91cf\uff0c\u5982\u679c\u4e3a1\uff0c\u5219\u5f80\u4e0b\u6267\u884c\u3002\u5982\u679c\u662f0\uff0c\u5219\u7b49\u5f85\u3002\nsemaphore.signal()\uff1a\u4ee3\u8868\u8fd0\u884c\u7ed3\u675f\uff0c\u4fe1\u53f7\u91cf\u52a01\uff0c\u6709\u7b49\u5f85\u7684\u4efb\u52a1\u8fd9\u4e2a\u65f6\u5019\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\n\n\/\/\u83b7\u53d6\u7cfb\u7edf\u5b58\u5728\u7684\u5168\u5c40\u961f\u5217\nlet queue = DispatchQueue.global(qos: .default)\n \n\/\/\u5f53\u5e76\u884c\u6267\u884c\u7684\u4efb\u52a1\u66f4\u65b0\u6570\u636e\u65f6\uff0c\u4f1a\u4ea7\u751f\u6570\u636e\u4e0d\u4e00\u6837\u7684\u60c5\u51b5\nfor i in 1...10 {\n    queue.async {\n        print(\"\\(i)\")\n    }\n}\n \n\/\/\u4f7f\u7528\u4fe1\u53f7\u91cf\u4fdd\u8bc1\u6b63\u786e\u6027\n\/\/\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u8ba1\u6570\u503c\u4e3a1\u7684\u4fe1\u53f7\nlet semaphore = DispatchSemaphore(value: 1)\nfor i in 1...10 {\n    queue.async {\n        \/\/\u6c38\u4e45\u7b49\u5f85\uff0c\u76f4\u5230Dispatch Semaphore\u7684\u8ba1\u6570\u503c >= 1\n        semaphore.wait()\n        print(\"\\(i)\")\n        \/\/\u53d1\u4fe1\u53f7\uff0c\u4f7f\u539f\u6765\u7684\u4fe1\u53f7\u8ba1\u6570\u503c+1\n        semaphore.signal()\n    }\n}<\/pre>\n\n\n\n<ul><li><strong>\u591a\u4e2a\u4efb\u52a1\u5168\u90e8\u7ed3\u675f\u540e\u505a\u4e00\u4e2a\u5168\u90e8\u7ed3\u675f\u7684\u5904\u7406<\/strong><\/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=\"\">async(group:)\uff1a\u7528\u6765\u76d1\u89c6\u4e00\u7ec4block\u5bf9\u8c61\u7684\u5b8c\u6210\uff0c\u4f60\u53ef\u4ee5\u540c\u6b65\u6216\u5f02\u6b65\u5730\u76d1\u89c6\nnotify()\uff1a\u7528\u6765\u6c47\u603b\u7ed3\u679c\uff0c\u6240\u6709\u4efb\u52a1\u7ed3\u675f\u6c47\u603b\uff0c\u4e0d\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\nwait()\uff1a\u7b49\u5f85\u76f4\u5230\u6240\u6709\u4efb\u52a1\u6267\u884c\u7ed3\u675f\uff0c\u4e2d\u9014\u4e0d\u80fd\u53d6\u6d88\uff0c\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\n\n\/\/\u83b7\u53d6\u7cfb\u7edf\u5b58\u5728\u7684\u5168\u5c40\u961f\u5217\nlet queue = DispatchQueue.global(qos: .default)\n\/\/\u5b9a\u4e49\u4e00\u4e2agroup\nlet group = DispatchGroup()\n\/\/\u5e76\u53d1\u4efb\u52a1\uff0c\u987a\u5e8f\u6267\u884c\nqueue.async(group: group) {\n    sleep(2)\n    print(\"block1\")\n}\nqueue.async(group: group) {\n    print(\"block2\")\n}\nqueue.async(group: group) {\n    print(\"block3\")\n}\n \n\/\/1,\u6240\u6709\u4efb\u52a1\u6267\u884c\u7ed3\u675f\u6c47\u603b\uff0c\u4e0d\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\ngroup.notify(queue: .global(), execute: {\n    print(\"group done\")\n})\n \n\/\/2,\u6c38\u4e45\u7b49\u5f85\uff0c\u76f4\u5230\u6240\u6709\u4efb\u52a1\u6267\u884c\u7ed3\u675f\uff0c\u4e2d\u9014\u4e0d\u80fd\u53d6\u6d88\uff0c\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\ngroup.wait()\nprint(\"\u4efb\u52a1\u5168\u90e8\u6267\u884c\u5b8c\u6210\")<\/pre>\n\n\n\n<ul><li><strong>\u6682\u505c\u6216\u8005\u7ee7\u7eed\u961f\u5217<\/strong><\/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=\"\">\u8fd9\u4e24\u4e2a\u51fd\u6570\u662f\u5f02\u6b65\u7684\uff0c\u800c\u4e14\u53ea\u5728\u4e0d\u540c\u7684blocks\u4e4b\u95f4\u751f\u6548\uff0c\u5bf9\u5df2\u7ecf\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1\u6ca1\u6709\u5f71\u54cd\u3002\nsuspend()\u540e\uff0c\u8ffd\u52a0\u5230Dispatch Queue\u4e2d\u5c1a\u672a\u6267\u884c\u7684\u4efb\u52a1\u5728\u6b64\u4e4b\u540e\u505c\u6b62\u6267\u884c\u3002\n\u800cresume()\u5219\u4f7f\u5f97\u8fd9\u4e9b\u4efb\u52a1\u80fd\u591f\u7ee7\u7eed\u6267\u884c\u3002\n\n\/\/\u521b\u5efa\u5e76\u884c\u961f\u5217\nlet conQueue = DispatchQueue(label: \"concurrentQueue1\", attributes: .concurrent)\n\/\/\u6682\u505c\u4e00\u4e2a\u961f\u5217\nconQueue.suspend()\n\/\/\u7ee7\u7eed\u961f\u5217\nconQueue.resume()<\/pre>\n\n\n\n<ul><li><strong>\u8fd0\u884c\u5728\u4e3b\u7ebf\u7a0b\u7684Main Dispatch Queue<\/strong><\/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=\"\">\u6b63\u5982\u540d\u79f0\u4e2d\u7684Main\u4e00\u6837\uff0c\u8fd9\u662f\u5728\u4e3b\u7ebf\u7a0b\u91cc\u6267\u884c\u7684\u961f\u5217\u3002\u56e0\u4e3a\u4e3b\u7ebf\u7a0b\u53ea\u6709\u4e00\u4e2a\uff0c\u6240\u6709\u8fd9\u81ea\u7136\u662f\u4e32\u884c\u961f\u5217\u3002\u4e00\u8d77\u8ddfUI\u6709\u5173\u7684\u64cd\u4f5c\u5fc5\u987b\u653e\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\nlet mainQueue = DispatchQueue.main<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6dfb\u52a0\u4efb\u52a1\u5230\u961f\u5217\u7684\u65b9\u6cd5\u00a0 \u53ea\u6267\u884c\u4e00\u6b21 asyncAfter\u00a0\u5ef6\u8fdf\u8c03\u7528 \u4fe1\u53f7\uff0c\u4fe1\u53f7\u91cf \u591a\u4e2a\u4efb\u52a1\u5168\u90e8\u7ed3\u675f\u540e\u505a\u4e00\u4e2a\u5168\u90e8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1188"}],"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=1188"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1188\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}