IOS Multithreading (Nsoperationqueue)

Source: Internet
Author: User

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)

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.