{"id":42533,"date":"2023-12-11T14:56:51","date_gmt":"2023-12-11T06:56:51","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=42533"},"modified":"2023-12-11T14:56:51","modified_gmt":"2023-12-11T06:56:51","slug":"%e4%bb%80%e4%b9%88%e6%98%afgcd-%ef%bc%9f%e8%b0%88%e8%b0%88%e4%bd%a0%e5%af%b9gcd%e7%9a%84%e7%90%86%e8%a7%a3%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e4%bb%80%e4%b9%88%e6%98%afgcd-%ef%bc%9f%e8%b0%88%e8%b0%88%e4%bd%a0%e5%af%b9gcd%e7%9a%84%e7%90%86%e8%a7%a3%ef%bc%9f\/","title":{"rendered":"\u4ec0\u4e48\u662fGCD \uff1f\u8c08\u8c08\u4f60\u5bf9GCD\u7684\u7406\u89e3\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    GCD\u662fGrand Central  Dispatch\u7684\u7f29\u5199\u3002\u3000\u3000<\/p>\n<p>Grand Central Dispatch (GCD)\u662fApple\u5f00\u53d1\u7684\u4e00\u4e2a\u591a\u6838\u7f16\u7a0b\u7684\u8f83\u65b0\u7684\u89e3\u51b3\u65b9\u6cd5\u3002\u5728Mac  OS X  10.6\u96ea\u8c79\u4e2d\u9996\u6b21\u63a8\u51fa\uff0c\u5e76\u5728\u6700\u8fd1\u5f15\u5165\u5230\u4e86iOS4.0\u3002\u3000\u3000<\/p>\n<p>GCD\u662f\u4e00\u4e2a\u66ff\u4ee3\u8bf8\u5982NSThread\u7b49\u6280\u672f\u7684\u5f88\u9ad8\u6548\u548c\u5f3a\u5927\u7684\u6280\u672f\u3002GCD\u5b8c\u5168\u53ef\u4ee5\u5904\u7406\u8bf8\u5982\u6570\u636e\u9501\u5b9a\u548c\u8d44\u6e90\u6cc4\u6f0f\u7b49\u590d\u6742\u7684\u5f02\u6b65\u7f16\u7a0b\u95ee\u9898\u3002<\/p>\n<p>GCD\u53ef\u4ee5\u5b8c\u6210\u5f88\u591a\u4e8b\u60c5\uff0c\u4f46\u662f\u8fd9\u91cc\u4ec5\u5173\u6ce8\u5728iOS\u5e94\u7528\u4e2d\u5b9e\u73b0\u591a\u7ebf\u7a0b\u6240\u9700\u7684\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\u3002\u3000\u3000<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u7406\u89e3\u662f\u8981\u63d0\u4f9b\u7ed9GCD\u961f\u5217\u7684\u662f\u4ee3\u7801\u5757\uff0c\u7528\u4e8e\u5728\u7cfb\u7edf\u6216\u8005\u7528\u6237\u521b\u5efa\u7684\u7684\u961f\u5217\u4e0a\u8c03\u5ea6\u8fd0\u884c\u3002 \u58f0\u660e\u4e00\u4e2a\u961f\u5217<\/p>\n<p>  \u5982\u4e0b\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7528\u6237\u521b\u5efa\u7684\u961f\u5217\uff1a <\/p>\n<p>&lt;pre&gt;&lt;code&gt;dispatch_queue_t myQueue =  dispatch_queue_create(&quot;&quot;com.iphonedevblog.post&quot;&quot;,  NULL);<\/p>\n<p>&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6807\u8bc6\u961f\u5217\u7684\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7528\u6765\u5b9a\u4e49\u961f\u5217\u7684\u53c2\u6570\uff08\u76ee\u524d\u4e0d\u652f\u6301\uff0c\u56e0\u6b64\u4f20\u5165NULL\uff09\u3002 <\/p>\n<p>&lt;strong&gt;\u6267\u884c\u4e00\u4e2a\u961f\u5217&lt;\/strong&gt; <\/p>\n<p>  \u5982\u4e0b\u4f1a\u5f02\u6b65\u6267\u884c\u4f20\u5165\u7684\u4ee3\u7801\uff1a <\/p>\n<p>  dispatch_async(myQueue, ^{ [self doSomething];  });\u5176\u4e2d\uff0c\u9996\u5148\u4f20\u5165\u4e4b\u524d\u521b\u5efa\u7684\u961f\u5217\uff0c\u7136\u540e\u63d0\u4f9b\u7531\u961f\u5217\u8fd0\u884c\u7684\u4ee3\u7801\u5757\u3002 <\/p>\n<p>&lt;strong&gt;\u58f0\u660e\u5e76\u6267\u884c\u4e00\u4e2a\u961f\u5217&lt;\/strong&gt; <\/p>\n<p>  \u5982\u679c\u4e0d\u9700\u8981\u4fdd\u7559\u8981\u8fd0\u884c\u7684\u961f\u5217\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e4b\u524d\u7684\u529f\u80fd\uff1a\u3000\u3000dispatch_async(dispatch_queue_create  (&quot;&quot;com.iphonedevblog.post&quot;&quot;, NULL), ^{ [self doSomething]; });\u5982\u679c\u9700\u8981\u6682\u505c\u4e00\u4e2a\u961f\u5217\uff0c\u53ef\u4ee5\u8c03\u7528\u5982\u4e0b\u4ee3\u7801\u3002\u6682\u505c\u4e00\u4e2a\u961f\u5217\u4f1a\u963b\u6b62\u548c\u8be5\u961f\u5217\u76f8\u5173\u7684\u6240\u6709\u4ee3\u7801\u8fd0\u884c\u3002\u3000\u3000dispatch_suspend(myQueue);<\/p>\n<p>&lt;strong&gt;\u6682\u505c\u4e00\u4e2a\u961f\u5217&lt;\/strong&gt; <\/p>\n<p>  \u5982\u679c\u6682\u505c\u4e00\u4e2a\u961f\u5217\u4e0d\u8981\u5fd8\u8bb0\u6062\u590d\u3002\u6682\u505c\u548c\u6062\u590d\u7684\u64cd\u4f5c\u548c\u5185\u5b58\u7ba1\u7406\u4e2d\u7684retain\u548crelease\u7c7b\u4f3c\u3002\u8c03\u7528dispatch_suspend\u4f1a\u589e\u52a0\u6682\u505c\u8ba1\u6570\uff0c\u800cdispatch_resume\u5219\u4f1a\u51cf\u5c11\u3002\u961f\u5217\u53ea\u6709\u5728\u6682\u505c\u8ba1\u6570\u53d8\u6210\u96f6\u7684\u60c5\u51b5\u4e0b\u624d\u5f00\u59cb\u8fd0\u884c\u3002dispatch_resume(myQueue);<\/p>\n<p>&lt;strong&gt;\u6062\u590d\u4e00\u4e2a\u961f\u5217&lt;\/strong&gt; &lt;strong&gt;\u4ece\u961f\u5217\u4e2d\u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c\u4ee3\u7801&lt;\/strong&gt; <\/p>\n<p>\u6709\u4e9b\u64cd\u4f5c\u65e0\u6cd5\u5728\u5f02\u6b65\u961f\u5217\u8fd0\u884c\uff0c\u56e0\u6b64\u5fc5\u987b\u5728\u4e3b\u7ebf\u7a0b\uff08\u6bcf\u4e2a\u5e94\u7528\u90fd\u6709\u4e00\u4e2a\uff09\u4e0a\u8fd0\u884c\u3002UI\u7ed8\u56fe\u4ee5\u53ca\u4efb\u4f55\u5bf9NSNotificationCenter\u7684\u8c03\u7528\u5fc5\u987b\u5728\u4e3b\u7ebf\u7a0b\u957f\u8fdb\u884c\u3002\u5728\u53e6\u4e00\u4e2a\u961f\u5217\u4e2d\u8bbf\u95ee\u4e3b\u7ebf\u7a0b\u5e76\u8fd0\u884c\u4ee3\u7801\u7684\u793a\u4f8b\u5982\u4e0b\uff1a\u3000\u3000dispatch_sync(dispatch_get_main_queue(),  ^{ [self dismissLoginWindow];  });\u6ce8\u610f\uff0cdispatch_suspend\uff08\u4ee5\u53cadispatch_resume\uff09\u5728\u4e3b\u7ebf\u7a0b\u4e0a\u4e0d\u8d77\u4f5c\u7528\u3002 <\/p>\n<p>\u4f7f\u7528GCD\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u7a0b\u5e8f\u4e0d\u4f1a\u5931\u53bb\u54cd\u5e94. \u591a\u7ebf\u7a0b\u4e0d\u5bb9\u6613\u4f7f\u7528\uff0c\u7528\u4e86GCD\uff0c\u4f1a\u8ba9\u5b83\u53d8\u5f97\u7b80\u5355\u3002\u4f60\u65e0\u9700\u4e13\u95e8\u8fdb\u884c\u7ebf\u7a0b\u7ba1\u7406, \u5f88\u68d2\uff01 <\/p>\n<p>&lt;pre&gt;&lt;code&gt;  dispatch_queue_t   t1=dispatch_queue_create(&quot;&quot;1&quot;&quot;  ,NULL);<br \/>\n  dispatch_queue_t   t2=dispatch_queue_create(&quot;&quot;2&quot;&quot;,NULL);<br \/>\n  dispatch_async(t1, ^{<br \/>\n      [self print1];<br \/>\n    });<br \/>\n  dispatch_async(t2, ^{<br \/>\n      [self print2];<br \/>\n    });<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; GCD\u662fGrand Central Dispatch\u7684\u7f29\u5199\u3002\u3000\u3000 Grand  [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-42533","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=42533"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42533\/revisions"}],"predecessor-version":[{"id":42534,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42533\/revisions\/42534"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=42533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=42533"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=42533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}