iOS Development Multithreaded Article-nsoperation Brief introduction
I. Introduction of Nsoperation
1. Brief description
Nsoperation?: Multi-threaded programming with nsoperation and Nsoperationqueue
Nsoperation and nsoperationqueue The specific steps for Multithreading:
(1) Encapsulate the action that needs to be performed into a Nsoperation object first
(2) Then add the Nsoperation object to the Nsoperationqueue
(3) The system will move the nsoperation out of the nsoperationqueue.
(4) Put the operation of the removed Nsoperation package into the new thread?
Sub-class of 2.NSOperation
Nsoperation is an abstract class, and does not have the ability to encapsulate operations, it must make it a subclass of
There are 3 ways to use the Nsoperation class:
(1) nsinvocationoperation
(2) Nsblockoperation
(3) Customize the subclass inheritance Nsoperation, and implement the internal corresponding method.
Ii. Specific Instructions
1.NSInvocationOperation sub-class
To create an object and perform an action:
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];
Note: Once the operation is done, the target's test method is called
code example:
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
Print View:
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
2.NSBlockOperation sub-class
To create an object and add an action:
1 //Create Nsblockoperation Action Object 2 nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{3 //...... 4 }]; 5 6 //Add Operation 7 [Operation addexecutionblock:^{8// .... 9 }];10
code example:
Code Listing 1:
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
Print View:
Code Listing 2:
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 35 //Open execution Operation [Operation Start];37}38 @end
Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1
3.NSOperationQueue
Nsoperationqueue: The nsoperation can be adjusted to start the task, but the default is synchronous execution
If you add nsoperation to the Nsoperationqueue (action queue), the system automatically executes the operations in Nsoperation asynchronously
Add actions to Nsoperationqueue, automate actions, automatically open threads
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]);
-(void) Addoperation: (Nsoperation *) op;
-(void) Addoperationwithblock: (void (^) (void)) block;
code example:
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
Printing effect:
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.
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.
The following uses a for loop to print, it is more obvious that the task is executed concurrently.
code example:
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
iOS Development Multithreaded Article 09-nsoperation Brief introduction