Step 6 Nsoperation (Basic walkthrough)

Source: Internet
Author: User

    • Nsoperation Abstract class
    • Nsoperation is an "abstract class" and cannot be used directly
    • The use of abstract classes is to define properties and methods common to subclasses
    • In Apple's head file, some abstract classes and subclasses are defined in the same header file.
    • Sub-class:

Nsinvocationoperation (Call)

Nsblockoperation (Block)

Nsoperationqueue queue

Basic Walkthrough

Nsinvocationoperation

Start

    • The Start method executes the @selector method on the current thread

-(void) OpDemo1 {

Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "Invocation"];

The Start method executes the @selector method on the current thread

[op start];

}

-(void) Downloadimage: (id) obj {

NSLog (@ "%@%@", [Nsthread CurrentThread], obj);

}

Add to queue

1 Adding an action to the queue will "asynchronously" execute the Selector method

-(void) OpDemo2 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "queue"];

[Q Addoperation:op];

}

Add multiple actions

-(void) OpDemo3 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

for (int i = 0; i < ++i) {

Nsinvocationoperation *op = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ (i)];

[Q Addoperation:op];

}

}

Execution effect: Multiple threads are turned on and not sequentially executed. Same as concurrent queue & asynchronous execution in GCD!

conclusion, in Nsoperation:

    • Tasks performed asynchronously with operations
    • Queues--Global queues

Nsblockoperation

-(void) OpDemo4 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "%@", [Nsthread CurrentThread]);

}];

[Q Addoperation:op];

}

Using block to define the operation, all the code is written together, simpler and easier to maintain!

more simple, add directly Block

-(void) OpDemo5 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

for (int i = 0; i < ++i) {

[Q addoperationwithblock:^{

NSLog (@ "%@%d", [Nsthread CurrentThread], i);

}];

}

}

Add a different action to the queue

-(void) OpDemo5 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

for (int i = 0; i < ++i) {

[Q addoperationwithblock:^{

NSLog (@ "%@%d", [Nsthread CurrentThread], i);

}];

}

Nsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "Block%@", [Nsthread CurrentThread]);

}];

[Q ADDOPERATION:OP1];

Nsinvocationoperation *OP2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downloadimage:) object:@ "Invocation"];

[Q ADDOPERATION:OP2];

}

    • You can add subclasses of any nsoperation to Nsoperationqueue

Inter-thread communication

-(void) OpDemo6 {

Nsoperationqueue *q = [[Nsoperationqueue alloc] init];

[Q addoperationwithblock:^{

NSLog (@ "time-consuming Operation%@", [Nsthread CurrentThread]);

Main thread Update UI

[[Nsoperationqueue Mainqueue] addoperationwithblock:^{

NSLog (@ "Update UI%@", [Nsthread CurrentThread]);

}];

}];

}

Step 6 Nsoperation (Basic walkthrough)

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.