1. Maximum number of concurrent:
-(Nsinteger) Maxconcurrentoperationcount;
-(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT;
Nsoperationqueue *queue = [[Nsoperationqueue alloc]init]; 3;
Note: The system is generally based on the size of the memory automatically set the number of concurrent, but also can set their own, but do not set up, generally not more than 5. 2. Suspension and cancellation
(1) Cancel all operations of the queue
-(void) cancelalloperations;
You can also call the Nsoperation-(void) Cancel method to cancel a single operation
(2) Pausing and resuming queues
-(void) setsuspended: (BOOL) b; Yes means pause queue, no for recovery queue
-(BOOL) issuspended; Current status
3. Operation Priority: Set nsoperation priority to change execution priority in Nsoperationqueue
[Operation2 setqueuepriority:nsoperationqueueprioritynormal]; Set Priority = operation2.queuepriority; Priority of the operation to be obtained
4. Operational dependencies
There are two operations, Operationa: Buy rice and operationb: Eat, must first buy rice, and then eat. Action B: Eat must wait until the operation a: the operation of the purchase of rice is completed before execution. This is the dependency relationship.
Description of dependency in nsoperation: [Operationb Adddependency:operationa];
- (void) viewdidload {[Super viewdidload]; Nsinvocationoperation*operationa = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (testaction)Object: nil]; Nsinvocationoperation*OPERATIONB = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (TestAction1)Object: nil]; [Operationb Adddependency:operationa]; //Defining QueuesNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; //add an action to a queue[Queue Addoperation:operationa]; [Queue addoperation:operationb]; }-(void) testaction{ for(inti =0; I <5; i + +) {NSLog (@"buy a meal for five people and bring it back to the dorm----%@", [Nsthread CurrentThread]); }}-(void) testaction1{ for(inti =0; I <5; i + +) {NSLog (@"Five people start eating----%@", [Nsthread CurrentThread]); }}
2017-06-18 11:33:26.765 demo[17639:3076261] Buy a meal for five people back to the dorm----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.766 demo[17639:3076261] Buy a meal for five people back to the dorm----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.767 demo[17639:3076261] Buy a meal for five people back to the dorm----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.768 demo[17639:3076261] Buy a meal for five people back to the dorm----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.769 demo[17639:3076261] Buy a meal for five people back to the dorm----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.770 demo[17639:3076261] Five people start eating----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.776 demo[17639:3076261] Five people start eating----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.777 demo[17639:3076261] Five people start eating----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.781 demo[17639:3076261] Five people start eating----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
2017-06-18 11:33:26.782 demo[17639:3076261] Five people start eating----<nsthread:0x600000267b40>{number = 3, name = (NULL)}
Note: The order in which the operations are executed is more visible with the for loop.
5. Monitoring of the operation
There are times when you need to perform an operation and then perform another operation, you can write these two actions together, but when the code is very large, it will not be very readable.
Nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{ for(inti =0; I <5; i + +) {NSLog (@"buy a meal for five people and bring it back to the dorm----%@", [Nsthread CurrentThread]); } for(inti =0; I <5; i + +) {NSLog (@"Five people start eating----%@", [Nsthread CurrentThread]); } }]; //Defining QueuesNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; //add an action to a queue[Queue addoperation:operation];
You can also write separately:
Nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{ for(inti =0; I <5; i + +) {NSLog (@"buy a meal for five people and bring it back to the dorm----%@", [Nsthread CurrentThread]); } }]; Operation.completionblock= ^{ for(inti =0; I <5; i + +) {NSLog (@"Five people start eating----%@", [Nsthread CurrentThread]); } }; //Defining QueuesNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; //add an action to a queue[Queue addoperation:operation];
2017-06-18 11:41:21.883 demo[17675:3084893] Buy a meal for five people back to the dorm----<nsthread:0x600000071200>{number = 3, name = (NULL)}
2017-06-18 11:41:21.885 demo[17675:3084893] Buy a meal for five people back to the dorm----<nsthread:0x600000071200>{number = 3, name = (NULL)}
2017-06-18 11:41:21.886 demo[17675:3084893] Buy a meal for five people back to the dorm----<nsthread:0x600000071200>{number = 3, name = (NULL)}
2017-06-18 11:41:21.888 demo[17675:3084893] Buy a meal for five people back to the dorm----<nsthread:0x600000071200>{number = 3, name = (NULL)}
2017-06-18 11:41:21.888 demo[17675:3084893] Buy a meal for five people back to the dorm----<nsthread:0x600000071200>{number = 3, name = (NULL)}
2017-06-18 11:41:21.894 demo[17675:3084895] Five people start eating----<nsthread:0x60800006c600>{number = 4, name = (NULL)}
2017-06-18 11:41:21.895 demo[17675:3084895] Five people start eating----<nsthread:0x60800006c600>{number = 4, name = (NULL)}
2017-06-18 11:41:21.896 demo[17675:3084895] Five people start eating----<nsthread:0x60800006c600>{number = 4, name = (NULL)}
2017-06-18 11:41:21.897 demo[17675:3084895] Five people start eating----<nsthread:0x60800006c600>{number = 4, name = (NULL)}
2017-06-18 11:41:21.898 demo[17675:3084895] Five people start eating----<nsthread:0x60800006c600>{number = 4, name = (NULL)}
Note: You can see that when the operation is completed, a new thread is opened to perform the meal operation.
Common operations for iOS multi-threaded---nsoperation