iOS Development--multi-threaded OC Articles & (10) multithreading nsoperation Basic use

Source: Internet
Author: User

Nsoperation Basic Operation

One, concurrent number(1) Concurrency: The number of tasks at the same time. For example, 3 threads are executing 3 tasks simultaneously, and the number of concurrency is 3 (2) Maximum concurrency: The maximum number of tasks that can be performed at the same time. (3) The most significant concurrent number of the relevant 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"23@interfaceYyviewcontroller ()45@end67@implementationYyviewcontroller89-(void) Viewdidload10{11[Super Viewdidload];1213//Creating Nsinvocationoperation objects, encapsulating operationsNsinvocationoperation *operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1)Object: nil];Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test2)Object: nil];16//Creating objects, encapsulating operationsNsblockoperation *operation3=[nsblockoperation blockoperationwithblock:^{18for (int i=0; i<5; i++) {NSLog (@"Nsblockoperation3--1----%@", [Nsthread CurrentThread]);20}21st}];[Operation3 addexecutionblock:^{23for (int i=0; i<5; i++) {NSLog (@"Nsblockoperation3--2----%@", [Nsthread CurrentThread]);25}26}];2728//Set Operation dependencies29//Execute the Operation2 first, then execute the Operation1, and finally execute the Operation330[Operation3 Adddependency:operation1];31[Operation1 Adddependency:operation2];3233//Can not be interdependent34//[Operation3 Adddependency:operation1];35//[Operation1 Adddependency:operation3];3637//Create NsoperationqueueNsoperationqueue * queue=[[Nsoperationqueue Alloc]init];39//Add an action to a queue40[Queue Addoperation:operation1];41[Queue Addoperation:operation2];42[Queue Addoperation:operation3];43}4445-(void) test146{47for (int i=0; i<5; i++) {NSLog (@"nsinvocationoperation--test1--%@", [Nsthread CurrentThread]);49}50}5152-(void53 {54 for (int i=0; I<) { 55 NSLog (@ " Nsinvocationoperation--test2--%@ ",[nsthread CurrentThread]); 56 }57 }58 59  @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"23@interfaceYyviewcontroller ()45@end67@implementationYyviewcontroller89-(void) Viewdidload10{11[Super Viewdidload];1213//Creating objects, encapsulating operationsNsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{NSLog (@"-operation-Download Picture-%@", [Nsthread CurrentThread]);16//..... What to do after downloading a pictureNSLog (@ " " Span style= "color: #000000;" >); 18 }]; 19 20 // Create queue 21 nsoperationqueue *queue=[[nsoperationqueue Alloc]init]; 22 // add tasks to the queue (auto-Execute, Automatic thread) 23  [queue Addoperation:operation];24 }25 26  @end                 

The second way:

1#import"YYViewController.h"23@interfaceYyviewcontroller ()45@end67@implementationYyviewcontroller89-(void) Viewdidload10{11[Super Viewdidload];1213//Creating objects, encapsulating operationsNsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{15for (int i=0; i<10; i++) {NSLog (@"-operation-Download Picture-%@", [Nsthread CurrentThread]);17}18}];1920//Execution of the listening operation is completeoperation.completionblock=^{22//..... What to do after downloading a pictureNSLog (@ " " Span style= "color: #000000;" >); 24 }; 25 26 // Create queue 27 nsoperationqueue *queue=[[nsoperationqueue Alloc]init]; 28 // add tasks to the queue (auto-Execute, Automatic thread) 29  [queue Addoperation:operation];30 }31 32  @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--multi-threaded OC Articles & (10) multithreading nsoperation Basic use

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.