iOS Development Multithreaded Article-nsoperation Brief introduction

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>iOS Development Multithreaded Article-nsoperation Brief introduction</strong></p></p><p><p>I. Introduction of Nsoperation</p></p><p><p>1. Brief description</p></p><p><p>Nsoperation?: multi-threaded programming with Nsoperation and Nsoperationqueue</p></p><p><p>Nsoperation and nsoperationqueue The specific steps for multithreading:</p></p><p><p>(1) encapsulate the action that needs to be performed into a Nsoperation object first</p></p><p><p>(2) then add the Nsoperation object to the Nsoperationqueue</p></p><p><p>(3) the system will move the nsoperation out of the Nsoperationqueue.</p></p><p><p>(4) put the operation of the removed Nsoperation package into the new thread?</p></p><p><p>Sub-Class of 2.NSOperation</p></p><p><p>Nsoperation is an abstract class, and does not have the ability to encapsulate operations, it must make it a subclass of</p></p><p><p>There are 3 ways to use the Nsoperation class:</p></p><p><p>(1) nsinvocationoperation</p></p><p><p>(2) nsblockoperation</p></p><p><p>(3) Customize the subclass inheritance nsoperation, and implement the internal corresponding Method.</p></p><p><p>Ii. Specific Instructions</p></p><p><p>1.NSInvocationOperation sub-class</p></p><p><p>To create an object and perform an action:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 //create an action object, encapsulate the task to be performed 2 //nsinvocationoperation encapsulation Operation 3 nsinvocationoperation *operation=[[ Nsinvocationoperation alloc]initwithtarget:self selector: @selector (test) object:nil];4 5 //perform action 6 [ Operation start];</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Note: once the operation is done, the Target's test method is called</p></p><p><p>Code example:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 01-nsoperation basic 1 4//5// Created by Hole Medical has on 14-6-25.6// Copyright (c ) 2014 Itcast. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 { [super viewdidload];20] //nsoperation: abstract class, No encapsulation function Create an Action object that encapsulates the task to be performed //nsinvocationoperation package operation Nsinvocationoperation *operation=[[ Nsinvocationoperation alloc]initwithtarget:self selector: @selector (test) object:nil];26 //perform Action 28 [operation start];29}31-(void) test33 {NSLog (@ "--test--%@--", [nsthread currentthread]); }37 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Print View:</p></p><p><p></p></p><p><p>Note: the Action object is executed by default in the main thread, and only new threads are opened when added to the Queue. That is, by default, if the action is not placed in the queue, it is performed synchronously. Operations are performed asynchronously only if the nsoperation is placed in a nsoperationqueue</p></p><p><p></p></p><p><p>2.NSBlockOperation sub-class</p></p><p><p>To create an object and add an action:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 //create nsblockoperation Action Object 2 nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{3 //...... 4 }]; 5 6 //add operation 7 [operation addexecutionblock:^{8// .... 9 }];10 </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Code example:</p></p><p><p>Code Listing 1:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 02-nstherad basic 2 4//5// Created by Hole doctor on 14-6-25.6// Copyright (c) 2 014 Years Itcast. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 { [super viewdidload];20 //create nsblockoperation Action Object Nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{23 NSLog (@ "nsblockoperation------%@" , [nsthread currentthread]); }];25 //open execution [operation start];29}30 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Print View:</p></p><p><p></p></p><p><p>Code Listing 2:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 02-nstherad basic 2 4//5// Created by Hole doctor on 14-6-25.6// Copyright (c) 2 014 Years Itcast. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 { [super viewdidload];20 //create nsblockoperation Action Object Nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{23 NSLog (@ "nsblockoperation------%@" , [nsthread currentthread]); }];25 //add operation [operation addexecutionblock:^{28 NSLog (@ "NSBlockOperation1------%@", [nsthread currentthread]); }];30 [operation addexecutionblock: ^{32 NSLog (@ "NSBlockOperation2------%@", [nsthread currentthread]), }];34 //open perform operation 36 [operation start];37}38 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1</p></p><p><p></p></p><p><p>3.NSOperationQueue</p></p><p><p>Nsoperationqueue: The nsoperation can be adjusted to start the task, but the default is synchronous execution</p></p><p><p>If you add nsoperation to the Nsoperationqueue (action queue), The system automatically executes the operations in Nsoperation asynchronously</p></p><p><p>Add actions to nsoperationqueue, automate actions, automatically open threads</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 //create nsoperationqueue 2 nsoperationqueue * queue=[[nsoperationqueue alloc]init]; 3 //add operation to queue 4 // The first way 5 [queue addoperation:operation1]; 6 [queue addoperation:operation2]; 7 [queue addoperation:o peration3]; 8 //second Way 9 [queue addoperationwithblock:^{10 NSLog (@ "nsblockoperation3--4----%@", [nsthread currentthread]); </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>-(void) addoperation: (nsoperation *) op;<br>-(void) addoperationwithblock: (void (^) (VOID)) block;</p></p><p><p>Code example:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1//2//YYVIEWCONTROLLER.M 3//03-nsoperation basic 3 4//5//Created by Hole Medical has on 14-6-25. 6//Copyright (c) 2014 Itcast. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontroller (void) viewDidLoad18 {[super viewdidload];20 21//create nsinvocationoperation object, Package operation: Nsinvocationopera tion *operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1) object:nil];23 Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test2) Object:nil];24//create object, Package Operation Nsblockoperation *operation3=[nsblockoperation blockoperationwithblock:^{26 N SLog (@ "nsblockoperation3--1----%@", [nsthread currentthread]);}];28 [operation3 addexecutionblock:^{29 N SLog (@ "nsblockoperation3--2----%@", [nsthread currentthread]); 30}];31 32//create NSOperationQueue33 nsoperatio Nqueue * Queue=[[nsoPerationqueue Alloc]init];34//add The operation to the queue [queue addoperation:operation1];36 [queue addoperation:operation2]; PNS [queue addoperation:operation3];38}39-(void) test141 {NSLog (@ "nsinvocationoperation--test1--%@", [nsthrea D currentthread]);}44-(void) test246 {NSLog (@ "nsinvocationoperation--test2--%@", [nsthread currentthread]); 48 }49 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Printing effect:</p></p><p><p></p></p><p><p>Note: the system automatically takes out the Nsoperation object in the Nsoperationqueue and puts its encapsulated operation into a new thread to Execute. In the above code example, there are four tasks, Operation1 and Operation2 have one task, and Operation3 has two tasks. Altogether four tasks, four threads were opened. The time to execute the task is all 273, as you can see, these tasks are executed in Parallel.</p></p>Note: the queue is out of order, and print results are not contradictory. This is like, the player A,BC although the starting order is first a, then b, then c, but the order to reach the end is not necessarily a B in front, C in the Rear.<p><p>The following uses a for loop to print, it is more obvious that the task is executed concurrently.</p></p><p><p>Code example:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 #import "YYViewController.h" 2 3 @interface yyviewcontroller () 4 5 @end 6 7 @implementation Yyviewcontroller 8 9- (void) viewDidLoad10 {one-to-one [super viewdidload];12 13//create nsinvocationoperation object, package operation: nsinvocationoperation *o Peration1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1) object:nil];15 Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test2) Object:nil];16//create object, Package Operation Nsblockoperation *operation3=[nsblockoperation blockoperationwithblock:^{18 F or (int i=0; i<5; i++) {NSLog (@ "nsblockoperation3--1----%@", [nsthread currentthread]); 20}21 }];22 [operation3 addexecutionblock:^{23 for (int i=0; i<5; i++) {NSLog (@ "nsblockoperation3--2- ---%@ ", [nsthread currentthread]); 25}26}];27 28//create NSOperationQueue29 nsoperationqueue * queue=[ [nsoperationqueue Alloc]init];30//add operation to the queue 31 [queue addoperation:operation1];32 [queue addoperation:operation2];33 [queue addoperation:operation3];34}35 36 -(void) test137 {i=0 for (int. i<5; i++) {NSLog (@ "nsinvocationoperation--test1--%@", [nsthread Currentthrea D]),}41}42-(void) test244 {$ for (int i=0; i<5; i++) {NSLog (@ "nsinvocationoperation--test2--%@", [nsthread currentthread]); }48}49 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p></p></p><p><p>iOS Development Multithreaded Article-nsoperation Brief introduction</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.