Association of multiple asynchronous operations with GCD and Nsoperation in iOS

Source: Internet
Author: User
Tags gcd
<span id="Label3"></p><p><p><span style="line-height: 1.5;">In the actual development of ios, we may encounter the following scenario: there are four operations a,b,c,d. requires that a,b,c be executed in a child thread, and that there may be a dependency between the a,b,c execution and the d,abc of the main thread. We can achieve this demand through GCD and Nsoperation.</span></p></p><p><p><span style="background-color: #3366ff;">1.GCD</span></p></p><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">with GCD</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) usegcd{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1.1 You can create a global concurrent queue, A,B,C operations are executed concurrently in child threads, and ABC does not have a sequential order of execution</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dispatch_queue_t quque=dispatch_get_global_queue (dispatch_queue_priority_default, 0); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1.2 You can also create a serial queue, and the a,b,c operation is executed serially in the child thread, a-b-c sequentially</span></span>dispatch_queue_t Quque=dispatch_queue_create (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">com.hzsy.www</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create a queue group</span></span>dispatch_group_t group=<span style="color: #000000;"><span style="color: #000000;">dispatch_group_create (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Enter Queue Group</span></span><span style="color: #000000;"><span style="color: #000000;">Dispatch_group_enter (group); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4. Adding a queue to a queue group and executing asynchronously</span></span>Dispatch_group_async (group, quque, ^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">a--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">a--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }); Dispatch_group_async (group, quque,</span></span>^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">b--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">b--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }); Dispatch_group_async (group, quque,</span></span>^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">c--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">c--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">5. When the action in the queue group is finished, go back to the main thread to perform the operation D</span></span>Dispatch_group_notify (group, Dispatch_get_main_queue (), ^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">d--finished-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">6. Leave the queue group</span></span><span style="color: #000000;"><span style="color: #000000;">Dispatch_group_leave (group); </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">Global Concurrent Queue print results: 2016-05-22 21:36:58.518 gcd and nsopration[797:144336] a--begain-<nsthread:0x15c51f170>{num ber = 2, name = (null)} 2016-05-22 21:36:58.525 gcd and nsopration[797:144335] b--begain-<nsthread:0x15c556a90>{num ber = 3, name = (null)} 2016-05-22 21:36:58.525 gcd and nsopration[797:144338] c--begain-<nsthread:0x15c54ac90>{num ber = 4, name = (null)} 2016-05-22 21:36:58.525 gcd and nsopration[797:144336] A--end-<nsthread:0x15c51f170>{number = 2, name = (null)} 2016-05-22 21:36:58.525 gcd and nsopration[797:144335] b--end-<nsthread:0x15c556a90>{number = 3, name = (null)} 2016-05-22 21:36:58.525 gcd and nsopration[797:144338] c--end-<nsthread:0x15c54ac90>{number = 4, Name = (null)} 2016-05-22 21:36:58.526 gcd and nsopration[797:144001] d--finished-<nsthread:0x15c50c380>{number = 1 , name = main}</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">Serial queue print results: 2016-05-22 21:39:20.660 gcd and nsopration[805:145035] A--begain-<nsthread:0x15c599cd0>{numbe r = 2, name = (null)} 2016-05-22 21:39:20.670 gcd and nsopration[805:145035] a--end-<nsthread:0x15c599cd0>{number = 2, name = (null)} 2016-05-22 21:39:20.670 gcd and nsopration[805:145035] b--begain-<nsthread:0x15c599cd0>{number = 2, name = (null)} 2016-05-22 21:39:20.670 gcd and nsopration[805:145035] b--end-<nsthread:0x15c599cd0>{number = 2, Name = (null)} 2016-05-22 21:39:20.671 gcd and nsopration[805:145035] c--begain-<nsthread:0x15c599cd0>{number = 2, Name = (null)} 2016-05-22 21:39:20.671 gcd and nsopration[805:145035] c--end-<nsthread:0x15c599cd0>{number = 2, na me = (null)} 2016-05-22 21:39:20.671 gcd and nsopration[805:145012] d--finished-<nsthread:0x15c604ba0>{number = 1, Name = main}</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;">}</span></span></pre><p><p></p></p><p><p><span style="background-color: #3366ff;">2.<span class="s1">nsoperationqueue</span></span></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller ()</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>@property (nonatomic, Strong) Nsoperationqueue *<span style="color: #000000;"><span style="color: #000000;">queue;</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">[super viewdidload];</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>Nsoperationqueue *queue=<span style="color: #000000;"><span style="color: #000000;">[[nsoperationqueue alloc]init];</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>_queue=<span style="color: #000000;"><span style="color: #000000;">queue;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></pre><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">with Nsoperationqueue</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) useoperationqueue{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Create an ABCD execution block</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>(^blocka) (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>)=^(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">a--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">a--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>(^blockb) (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>)=^(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">b--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">b--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>(^blockc) (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>)=^(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">c--begain-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">c--end-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>(^blockd) (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>)=^(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">d--finished-%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [nsthread currentthread]); }; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create an ABCD nsblockoperation operation Block</span></span>Nsblockoperation *opa=<span style="color: #000000;"><span style="color: #000000;">[nsblockoperation blockoperationwithblock:blocka]; Nsblockoperation</span></span>*opb=<span style="color: #000000;"><span style="color: #000000;">[nsblockoperation blockoperationwithblock:blockb]; Nsblockoperation</span></span>*opc=<span style="color: #000000;"><span style="color: #000000;">[nsblockoperation blockoperationwithblock:blockc]; Nsblockoperation</span></span>*opd=<span style="color: #000000;"><span style="color: #000000;">[nsblockoperation blockoperationwithblock:blockd]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3.1 Setting the dependency of the D operation</span></span><span style="color: #000000;"><span style="color: #000000;">[opD adddependency:opa]; [opD adddependency:opb]; [opD adddependency:opc]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3.2 Set the dependency between ABC and if not set, ABC will execute concurrently</span></span><span style="color: #000000;"><span style="color: #000000;">[opB adddependency:opa]; [opC adddependency:opb]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4. Add ABC operations to the queue</span></span><span style="color: #000000;"><span style="color: #000000;">[_queue addoperation:opa]; [_queue addoperation:opb]; [_queue addoperation:opc]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">5. Add d to the main queue</span></span><span style="color: #000000;"><span style="color: #000000;">[[nsoperationqueue mainqueue] addoperation:opd]; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">set dependent execution results between ABC execution results: 2016-05-22 21:55:29.114 gcd and nsopration[828:148402] a--begain-<nsthread:0x147556cc0 >{number = 2, name = (null)} 2016-05-22 21:55:29.127 gcd and nsopration[828:148402] A--end-<nsthread:0x147556cc0> ; {number = 2, name = (null)} 2016-05-22 21:55:29.128 gcd and nsopration[828:148402] b--begain-<nsthread:0x147556cc0> {number = 2, name = (null)} 2016-05-22 21:55:29.128 gcd and nsopration[828:148402] B--end-<nsthread:0x147556cc0>{nu mber = 2, name = (null)} 2016-05-22 21:55:29.130 gcd and nsopration[828:148402] C--begain-<nsthread:0x147556cc0>{nu mber = 2, name = (null)} 2016-05-22 21:55:29.130 gcd and nsopration[828:148402] C--end-<nsthread:0x147556cc0>{numbe r = 2, name = (null)} 2016-05-22 21:55:29.130 gcd and nsopration[828:148377] d--finished-<nsthread:0x14750c3e0>{num ber = 1, name = main}</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">do not set dependent execution results between abc: 2016-05-22 21:56:44.480 gcd and nsopration[833:148926] A--begain-<nsthread:0x1265a3730> ; {number = 2, name = (null)} 2016-05-22 21:56:44.480 gcd and nsopration[833:148929] b--begain-<nsthread:0x12658f4a0> {number = 3, name = (null)} 2016-05-22 21:56:44.480 gcd and nsopration[833:148927] c--begain-<nsthread:0x1265a2ea0> {number = 4, name = (null)} 2016-05-22 21:56:44.484 gcd and nsopration[833:148926] A--end-<nsthread:0x1265a3730>{nu mber = 2, name = (null)} 2016-05-22 21:56:44.485 gcd and nsopration[833:148929] B--end-<nsthread:0x12658f4a0>{numbe r = 3, name = (null)} 2016-05-22 21:56:44.485 gcd and nsopration[833:148927] c--end-<nsthread:0x1265a2ea0>{number = 4, name = (null)} 2016-05-22 21:56:44.485 gcd and nsopration[833:148902] D--finished-<nsthread:0x12650c3c0>{number = 1, name = main}</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #000000;"><span style="color: #000000;"> }</span></span></pre><p><p>From the printed results above, you can see that if a dependency is set, only one child thread will be created. if not set, a multi-threaded thread is Created.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>---restore content ends---</p></p><p><p>Association of multiple asynchronous operations with GCD and Nsoperation in iOS</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.