iOS Development Multithreading Chapter 10-nsoperation Basic Operations

Source: Internet
Author: User

iOS Development Multithreading Chapter-nsoperation Basic Operations

One, concurrent number(1) Number of concurrent: the number of tasks at the same time. For example, open 3 threads at the same time to perform 3 tasks, and the concurrency number is 3.(2) Max concurrency: The maximum number of tasks that can be performed at the same time. (3) The most significant concurrent number of the correlation? Method-(Nsinteger) Maxconcurrentoperationcount;
-(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT; Note: If the maximum concurrency number is not set, then the number of concurrent is determined by the system memory and CPU, may be a little more memory, less memory to open less. Note: The value of NUM does not represent the number of threads, only the ID of the thread. tip: The maximum number of concurrent do not write (less than 5), do not open too much, generally to both of the appropriate, because although the task is processed on the child thread, but the CPU processing these excessive child threads may affect the UI, let the UI card.   Ii. cancellation, suspension and recovery of the queue

(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) The application of pause and resume: In the TableView interface, the remote Web interface can be downloaded remotely, which will affect the UI and make the user experience worse. In this case, you can set the queue to pause (not cancel the queue) when the user operates the UI (such as scrolling the screen), to stop scrolling, and to resume the queues.   third, the operation priority level

(1) Set the priority of Nsoperation in queue, can change the operation of the priority level

-(nsoperationqueuepriority) queuepriority;
-(void) Setqueuepriority: (nsoperationqueuepriority) p;

(2) The value of the priority level

Nsoperationqueuepriorityverylow = -8l,

Nsoperationqueueprioritylow = -4l,

Nsoperationqueueprioritynormal = 0,

Nsoperationqueuepriorityhigh = 4,

Nsoperationqueuepriorityveryhigh = 8

Description: A task with high priority will have a greater chance of being called.

Iv. Operational Dependencies

(1) Nsoperation can be set up between dependencies to ensure the order of execution,? If you must let operation a finish, you can perform operation B, as follows

[Operationb Adddependency:operationa]; Action B depends on the action

(2) You can create a dependency between nsoperation of different queue

Note: You cannot cycle a dependency (a depends on b,b and a).

(3) 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//Set operation dependent 29//execute Operation2 First, then execute Operation1, last execution oper ation330 [Operation3 adddependency:operation1];31     [Operation1 adddependency:operation2];32 33//cannot be mutually dependent on//[Operation3 adddependency:operation1];35//  [Operation1 adddependency:operation3];36 37//Create NSOperationQueue38 Nsoperationqueue * queue=[[nsoperationqueue  alloc]init];39//Add operation to the queue. [Queue addoperation:operation1];41 [queue addoperation:operation2];42 [queue] addoperation:operation3];43}44-(void) test146 {A for (int i=0; i<5; i++) {$ NSLog (@ "Nsinvocationoperatio n--test1--%@ ", [Nsthread CurrentThread]),}50}51-(void) test253 {(int i=0; i<5; i++) {NSLog ( @ "nsinvocationoperation--test2--%@", [Nsthread CurrentThread]);}57}58 @end
 Print View:a finish doing b,b finish before doing C. Note: Be sure to set it before adding it. Tip: The order in which tasks are added does not dictate the order of execution, and the order of execution depends on dependencies. The purpose of using operation is to let developers no longer care about threads.   5. Monitoring of the Operation

Can listen for an operation completed

-(void (^) (void)) Completionblock;
-(void) Setcompletionblock: (void (^) (void)) block;

code example

The first way: You can directly follow the task to write the need to complete the operation, such as here after downloading the picture, followed by the download of the second picture. But this writing sometimes put two unrelated operations into a code block, the code is not very readable.

1 #import "YYViewController.h" 2  3 @interface Yyviewcontroller () 4  5 @end 6  7 @implementation Yyviewcontroll ER 8  9-(void) VIEWDIDLOAD10 {One-to-one     [super viewdidload];12     //Create object, encapsulation operation,     nsblockoperation *operation= [Nsblockoperation blockoperationwithblock:^{15         NSLog (@ "-operation-download picture-%@", [Nsthread CurrentThread]); 16         //..... To continue the operation after downloading the picture         NSLog (@ "--then download the second picture--");     }];19     //Create queue     nsoperationqueue *queue=[[ Nsoperationqueue alloc]init];22     //Add tasks to the queue (auto-execute, auto-thread)     [queue addoperation:operation];24}25 @end

The second way:

1 #import "YYViewController.h" 2  3 @interface Yyviewcontroller () 4  5 @end 6  7 @implementation Yyviewcontroll ER 8  9-(void) VIEWDIDLOAD10 {One-to-one     [super viewdidload];12     //Create object, encapsulation operation,     nsblockoperation *operation= [Nsblockoperation blockoperationwithblock:^{15         for (int i=0; i<10; i++) {             NSLog (@ "-operation-download picture-%@", [Nsthread CurrentThread]);         }18     }];19     //monitoring operation completed     operation.completionblock=^{22         //... To continue the operation after downloading the picture         NSLog (@ "--then download the second picture--");     };25     //Create queue     nsoperationqueue *queue=[[ Nsoperationqueue alloc]init];28     //Add tasks to the queue (auto-execute, auto-thread)     [queue addoperation:operation];30}31 @end

Print View:

Description: After the last task execution, the operation.completionblock=^{} code snippet is executed and executed on the current thread (2).

iOS Development Multithreading Chapter 10-nsoperation Basic Operations

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.