2016-1-19 Preliminary study on Nsoperation

Source: Internet
Author: User

A: Introduction

The role of 1.NSOperation:

With Nsoperation and Nsoperationqueue can also realize multithreading.

2.NSOperation and nsoperationqueue the steps to implement multithreading:

2.1 Now encapsulate the required actions into the Nsoperation object

2.2 Then add the nsoperation to the Nsoperationqueue

2.3 The system will automatically remove the nsoperation from the Nsoperationqueue

II: Subclasses of the Nsoperation

1.NSOPeration is an abstract class that does not have the ability to encapsulate operations and must use his subclasses.

2. There are three ways to use the Nsoperation subclass:

2.1NSInvocationOpertation

2.2NSBlockOperating

2.3 Customizing subclass Inheritance Nsoperation, implementing internal corresponding methods

Three: Nsoperationqueue

Type of 1.NSOperationQueue:

1.1 Main queue, via [nsoperationqueue mainqueue]; available.

1.2 Other queues: Serial queue, concurrent queue. Available through [[nsoperationqueue alloc ] init];

2. Use:

- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{nsoperationqueue*OPQ =[[Nsoperationqueue alloc] init]; Nsinvocationoperation*OP1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (RUN1)Object: nil]; Nsinvocationoperation*OP2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (RUN2)Object: nil]; [OpQ ADDOPERATION:OP1];//The [OP1 start] method is called internally and placed in the sub-line range[OpQ addoperation:op2];}- (void) run1{NSLog (@"1------%@", [Nsthread CurrentThread]);}- (void) run2{NSLog (@"2------%@", [Nsthread CurrentThread]);}

3. Custom operation

3.1 Usage Scenarios: When the operation is more complex, you can customize a nsoperation, and then put the complex operations in the custom operation.

3.2 Implementations:

3.2.1 needs to override the main function in the custom operation:

Such as:

-(void) main{    NSLog (@ "%@-----------------custom Operation", [ Nsthread CurrentThread]);}

3.2.2 Create a custom operation to add to the queue:

Zznsopertation *op3 = [[zznsopertation alloc] init];

[OpQ addoperation: op3];

3.2.3 Printing results:

2016-01-19 17:43:39.941 nsperationqueue[16438:1477860] <nsthread:0x7ff00a60d600>{number = 4, name = (NULL)}----- ------------Custom Operation

2016-1-19 Preliminary study on 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.