1. Basic use
Nsinvocationoperation
1. Create an Action object that encapsulates the task that needs to be performed Nsinvocationoperation * Operation =[[nsinvocationoperation alloc]initwithtarget:self Selector: @selector (download) Object:nil]; 2. Perform the action (by default, if the action is not placed in the queue, it is synchronous) [Operation start];-(void) download{ NSLog (@ "--------Download picture-----%@" , [Nsthread CurrentThread]);}
Nsblockoperation
1. Package Operation Nsblockoperation * operation=[nsblockoperation blockoperationwithblock:^{ NSLog (@ "-------- Download picture-----%@ ", [Nsthread CurrentThread]); }]; [Operation addexecutionblock:^{ NSLog (@ "--------Download picture 1-----%@", [Nsthread CurrentThread]); }]; [Operation addexecutionblock:^{ NSLog (@ "--------Download picture 2-----%@", [Nsthread CurrentThread]); }]; 2. Perform the Operation [operation start]; Note: An operation is performed asynchronously as long as the nsblockoperation encapsulates the operand >1
Nsoperationqueue Operation queue
Create an action queue nsoperationqueue * Queue =[[nsoperationqueue alloc]init]; Nsinvocationoperation *operation1 =[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (download) Object:nil]; Nsinvocationoperation * Operation2 =[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (Run) object : nil]; Nsblockoperation * Operation3 =[nsblockoperation blockoperationwithblock:^{ NSLog (@ "--------Download picture 2-----%@", [ Nsthread CurrentThread]); }]; [Operation3 addexecutionblock:^{ NSLog (@ "--------run2-----%@", [Nsthread CurrentThread]); }]; [Queue Addoperation:operation1]; [Queue Addoperation:operation2]; [Queue Addoperation:operation3]; Put the action in the queue to operate asynchronously
* Set the maximum number of concurrent
-(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT;
* Set Dependencies
[Opetationb adddependency:operationa];//Operation B relies on operation a a executes before execution B
IOS Multithreading (Nsoperationqueue)