Thread 9--nsoperation

Source: Internet
Author: User

I. Introduction of Nsoperation

1. Brief description

Nsoperation?: Multi-threaded programming with nsoperation and Nsoperationqueue

Nsoperation and nsoperationqueue The specific steps for Multithreading:

(1) Encapsulate the action that needs to be performed into a Nsoperation object first

(2) Then add the Nsoperation object to the Nsoperationqueue

(3) The system will move the nsoperation out of the nsoperationqueue.

(4) Put the operation of the removed Nsoperation package into the new thread?

Sub-class of 2.NSOperation

Nsoperation is an abstract class, and does not have the ability to encapsulate operations, it must make it a subclass of

There are 3 ways to use the Nsoperation class:

(1) nsinvocationoperation

(2) Nsblockoperation

(3) Customize the subclass inheritance Nsoperation, and implement the internal corresponding method.

/*************************************************************/    //that is, by default, if the action is not placed in the queue, it is performed synchronously. Operations are performed asynchronously only if the nsoperation is placed in a nsoperationqueueNSLog (@"Main thread---%@", [Nsthread CurrentThread]); Nsinvocationoperation*operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1)Object: nil]; [Operation1 start];/*************************************************************/   //Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1Nsblockoperation *operation2=[nsblockoperation blockoperationwithblock:^{         for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation1----%@", [Nsthread CurrentThread]);}        }]; [Operation2 Addexecutionblock:^{         for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation2----%@", [Nsthread CurrentThread]);}    }]; [Operation2 Addexecutionblock:^{         for(intI=1; i<5; i++) {NSLog (@"NSBlockOperation3----%@", [Nsthread CurrentThread]);    }    }]; //This sentence is not required after being put into the queue;[Operation2 start];    /*************************************************************/    /*1) First encapsulate the required action into a Nsoperation object (2) and then add the Nsoperation object to the Nsoperationqueue (3) The system will move nsoper in the Nsoperationqueue    Ation take Out (4) put the removed Nsoperation encapsulated operation into the new thread? */nsinvocationoperation*operation3=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (TEST3)Object: nil]; Nsinvocationoperation*operation4=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (TEST4)Object: nil]; Nsoperationqueue*queue=[[Nsoperationqueue alloc]init];    [Queue Addoperation:operation2];    [Queue Addoperation:operation3];    [Queue Addoperation:operation4]; 
/*************************************************************/-(void) test1{NSLog (@"operation1--test1---%@", [Nsthread CurrentThread]);}-(void) test3{ for(intI=1; i<5; i++) {NSLog (@"nsoperationqueue-test3----%@", [Nsthread CurrentThread]); }    }-(void) test4{ for(intI=1; i<5; i++) {NSLog (@"nsoperationqueue--test4---%@", [Nsthread CurrentThread]); }}

Thread 9--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.