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.
/*************************************************************/ //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 nsoperationqueueNSLog (@"Main thread---%@", [Nsthread CurrentThread]); Nsinvocationoperation*operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1)Object: nil]; [Operation1 start];/*************************************************************/ //Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1Nsblockoperation *operation2=[nsblockoperation blockoperationwithblock:^{ for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation1----%@", [Nsthread CurrentThread]);} }]; [Operation2 Addexecutionblock:^{ for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation2----%@", [Nsthread CurrentThread]);} }]; [Operation2 Addexecutionblock:^{ for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation3----%@", [Nsthread CurrentThread]); } }]; //This sentence is not required after being put into the queue;[Operation2 start]; /*************************************************************/ /*1) First encapsulate the required action into a Nsoperation object (2) and then add the Nsoperation object to the Nsoperationqueue (3) The system will move nsoper in the Nsoperationqueue Ation take Out (4) put the removed Nsoperation encapsulated operation into the new thread? */nsinvocationoperation*operation3=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (TEST3)Object: nil]; Nsinvocationoperation*operation4=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (TEST4)Object: nil]; Nsoperationqueue*queue=[[Nsoperationqueue alloc]init]; [Queue Addoperation:operation2]; [Queue Addoperation:operation3]; [Queue Addoperation:operation4];
/*************************************************************/-(void) test1{NSLog (@"operation1--test1---%@", [Nsthread CurrentThread]);}-(void) test3{ for(intI=1; i<5; i++) {NSLog (@"nsoperationqueue-test3----%@", [Nsthread CurrentThread]); } }-(void) test4{ for(intI=1; i<5; i++) {NSLog (@"nsoperationqueue--test4---%@", [Nsthread CurrentThread]); }}
Thread 9--nsoperation