Threading operations in iOS (3)

Source: Internet
Author: User
<span id="Label3"></p><p><p>Using CGD is more efficient than the previous two (it is said that interested students can try It). This is also the way that Apple is more recommended to Use.<br>GCD is the abbreviation for Grand Central dispatch, A set of C interfaces for implementing concurrent Programming. GCD was developed based on the characteristics of the objective-c block, and the basic business logic is similar to Nsoperation. is to add a task to a queue, which is responsible for thread generation and scheduling by the System. Because of the direct use of block, it is very convenient to use, reducing the threshold of multi-threaded Development.</p></p><p><p>Or the first two examples of downloading pictures, let's take a look at what to do with the GCD Mode.</p></p><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-preprocessor"><span class="hljs-preprocessor">#import <span class="hljs-title">"ViewController.h"</span> </span></span><span class="hljs-preprocessor"><span class="hljs-preprocessor">#define IMAGE_PATH @ "https://www.baidu.com/img/bdlogo.png"</span></span><span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">@interface</span> <span class="hljs-title">viewcontroller</span> ()</span></span><span class="hljs-keyword"><span class="hljs-keyword">@property</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">Weak</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">nonatomic</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">Iboutlet</span></span> <span class="hljs-built_in"><span class="hljs-built_in">Uiimageview</span></span>*mimage;<span class="hljs-keyword"><span class="hljs-keyword">@end</span></span><span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">@implementation</span> <span class="hljs-title">viewcontroller</span> </span></span>- (<span class="hljs-keyword"><span class="hljs-keyword">void</span></span>) Viewdidload {[<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>viewdidload];<span class="hljs-built_in"><span class="hljs-built_in">Dispatch_async</span></span>(dispatch_get_global_queue (dispatch_queue_priority_background,<span class="hljs-number"><span class="hljs-number">0</span></span>), ^{nsdata *imagedata = [[nsdata alloc] initwithcontentsofurl:[<span class="hljs-built_in"><span class="hljs-built_in">Nsurl</span></span>urlwithstring:image_path]];<span class="hljs-built_in"><span class="hljs-built_in">UIImage</span></span>*image = [[<span class="hljs-built_in"><span class="hljs-built_in">UIImage</span></span>alloc] initwithdata:imagedata];<span class="hljs-comment"><span class="hljs-comment">//back to main</span> thread</span> <span class="hljs-built_in"><span class="hljs-built_in">Dispatch_async</span></span>(dispatch_get_main_queue (), ^{[<span class="hljs-keyword"><span class="hljs-keyword"></span> self</span><span class="hljs-variable"><span class="hljs-variable">. Mimage</span></span>setimage:image]; }); });}</code></pre><p><p>is not much more concise, GCD will automatically help us create message queue, do not need us to manually create and Manage.<br>The system provides three concurrent dispatch queues for each Application. These three concurrent dispatch queues are global, and they differ only by Priority. Because it's global, we don't need to create it. We only need to get the queue by using the function dispath_get_global_queue, as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs fix"><span class="hljs-attribute">dispatch_queue_t globalQ </span>=<span class="hljs-string"> dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); </span></code></pre></pre><p><p>2, about the use of Dispatch_group_async<br>Dispatch_group_async can be used to listen to whether a group of tasks are completed and be notified to perform other operations when Completed. This method is useful, for example, you perform three download tasks, and when three tasks are downloaded, you are notified that the interface is Complete. Here is an example code:</p></p><pre class="prettyprint"><code class=" hljs objectivec"> <span class="hljs-built_in"><span class="hljs-built_in">dispatch_queue_t</span></span>Queue = Dispatch_get_global_queue (dispatch_queue_priority_default,<span class="hljs-number"><span class="hljs-number">0</span></span>); dispatch_group_t Group = dispatch_group_create (); Dispatch_group_async (group, queue,^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">1.0</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"1.0"</span></span>); }); Dispatch_group_async (group, queue,^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">2.0</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"2.0"</span></span>); }); Dispatch_group_async (group, queue,^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">3.0</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"3.0"</span></span>); }); Dispatch_group_notify (group, dispatch_get_main_queue (), ^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"dispatch_group_async"</span></span>); });</code></pre><p><p>Printing results:<br>2015-06-03 12:42:46.673 iOS Multithreading [1706:138,779] 1.0<br>2015-06-03 12:42:47.671 iOS Multithreading [1706:138,782] 2.0<br>2015-06-03 12:42:48.672 iOS Multithreading [1706:138,787] 3.0<br>2015-06-03 12:42:48.672 iOS Multithreading [1706:138679] dispatch_group_async</p></p><p><p>3, Dispatch_barrier_async<br>We can pass</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-built_in">dispatch_queue_t</span> queue = dispatch_queue_create(<span class="hljs-keyword">const</span><span class="hljs-keyword">char</span> *label, dispatch_queue_attr_t attr)</code></pre></pre><p><p>method to generate a custom queue, a label parameter, which can theoretically be used with any custom value, we generally use the reverse program path Name.<br>Look at the following code:</p></p><pre class="prettyprint"><code class=" hljs objectivec"> <span class="hljs-built_in"><span class="hljs-built_in">dispatch_queue_t</span></span>Queue = Dispatch_queue_create (<span class="hljs-string"><span class="hljs-string">"asd"</span></span>, dispatch_queue_serial);<span class="hljs-built_in"><span class="hljs-built_in">Dispatch_async</span></span>(queue, ^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">2</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"1"</span></span>); });<span class="hljs-built_in"><span class="hljs-built_in">Dispatch_async</span></span>(queue, ^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">4</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"2"</span></span>); }); Dispatch_barrier_async (queue, ^{<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"3"</span></span>); });<span class="hljs-built_in"><span class="hljs-built_in">Dispatch_async</span></span>(queue, ^{[<span class="hljs-built_in"><span class="hljs-built_in">Nsthread</span></span>Sleepfortimeinterval:<span class="hljs-number"><span class="hljs-number">1</span></span>];<span class="hljs-built_in"><span class="hljs-built_in">NSLog</span></span>(@<span class="hljs-string"><span class="hljs-string">"4"</span></span>); });</code></pre><p><p>Printing results:<br>2015-06-03 13:05:37.753 iOS Multithreading [1989:151,430] 1<br>2015-06-03 13:05:41.757 iOS Multithreading [1989:151,430] 2<br>2015-06-03 13:05:45.763 iOS Multithreading [1989:151,430] 3<br>2015-06-03 13:05:46.764 iOS Multithreading [1989:151,430] 4</p></p><p><p>You can see that the print results are printed sequentially, which means that this is a serial operation to be performed on the sub-thread, Dispatch_barrier_async is executed after the execution of the previous task, and the task after it is completed before it Executes.</p></p> <p><p>Threading operations in iOS (3)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.