Common operations for iOS multi-threaded---nsoperation

Source: Internet
Author: User

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

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.