iOS Development nsoperation & Nsoperationqueue

Source: Internet
Author: User
Tags gcd image filter

1 , Introduction

(1) Nsoperationqueue (Operation queue) is the cocoa abstraction of the queue model provided by GCD, a set of objective-c APIs that makes concurrent (multithreaded) programming easier, but less efficient than GCD. in the actual development Nsoperationqueue is preferred.

(2) GCD provides a lower level of control, while the operations queue implements some convenient functions on top of GCD, which are often the best and safest choice for developers.

Queues and operations

(1) Nsoperationqueue has two different types of queues: Primary queue and Custom queue

(2) The home row runs on the main thread.

(3) Custom queues are executed in the background (as long as the custom queue is concurrent).

(4) nsoperation is not used directly, the task of queue processing is nsoperation sub-class:

(a) Nsinvocationoperation

(b) Nsblockoperation

2 , nsoperation basic steps to use

Basic use Steps

(1) Defining the Operation queue

(2) Define the operation

(3) Adding an action to a queue

Tip: Once the operation is added to the queue, the operation is immediately scheduled for execution

3 , nsinvocationoperation ( dispatch Operation )

To define a queue:

Self.myqueue = [[Nsoperationqueue alloc] init];

Method of operation invocation:

-(void) Operationaction: (ID) obj

{

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

}

Define the action and add to the queue:

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

[Self.myqueue Addoperation:op];

For example:

Where Myqueue is:

@property (nonatomic, strong) Nsoperationqueue *myqueue;

-(void) DEMOOP2

{

You need to define a method that can receive a parameter

It's not flexible enough to use.

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

[Self.myqueue addoperation:op];//running on a child thread

[[Nsoperationqueue Mainqueue] addoperation:op];//runs on the main thread

}

4 , Nsblockoperation ( block Operation )

Define actions and add to queues

Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{

[Self operationaction:@ "Block operation"];

}];

Add an action to a queue

[Self.myqueue Addoperation:op];

"Remarks" Nsblockoperation more flexible than nsinvocationoperation

For example:

Where Myqueue is:

@property (nonatomic, strong) Nsoperationqueue *myqueue;

#pragma mark sets the order in which tasks are executed

-(void) demoOp3

{

Nsblockoperation *OP1 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP2 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP3 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP4 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

Set execution order, dependency dependent, system may open multiple threads, but not too much

Dependencies can be cross-queued!

[Op2 adddependency: OP1];

[Op3 ADDDEPENDENCY:OP2];

[OP4 ADDDEPENDENCY:OP3];

GCD is a serial queue, an asynchronous task that only opens a thread

[Self.myqueue ADDOPERATION:OP1];

[Self.myqueue ADDOPERATION:OP2];

[Self.myqueue ADDOPERATION:OP3];

all UI updates need to be made on the main thread so that the OP4 is executed on the main thread

[[Nsoperationqueue Mainqueue] ADDOPERATION:OP4];

}

5 , set the dependencies for the Operation

Nsblockoperation *OP1 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP2 =

[Nsblockoperation blockoperationwithblock:^{

NSLog (@ "%@-add image Filter", [Nsthread CurrentThread]);

}];

Nsblockoperation *OP3 =

[Nsblockoperation blockoperationwithblock:^{

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

}];

[OP1 ADDDEPENDENCY:OP3]; will cause cyclic dependence

[Op2 ADDDEPENDENCY:OP1];

[Op3 ADDDEPENDENCY:OP2];

[Self.myqueue ADDOPERATION:OP1];

[Self.myqueue ADDOPERATION:OP2];

[[Nsoperationqueue Mainqueue] addoperation:op3];

Tip: You can use adddependency To specify the dependencies between Operations ( execution Sequence )

Note: Do not appear circular dependent!

6 , set the number of concurrent threads concurrently

Setting the number of concurrent threads can effectively reduce CPU and memory overhead, which is not easily implemented with GCD.

New threads are cost-aware

When setting the maximum number of simultaneous concurrent threads, if the previous thread has finished working but has not yet been destroyed, a new thread is created

Application Scenario: Web development, download work! (thread cost: CPU,MEM) Power!

If it is 3G, open 3 sub-threads

If WiFi, open 6 sub-threads

[Self.myqueue setmaxconcurrentoperationcount:2];

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

Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{

[Self operationaction:@ (i)];

}];

[Self.myqueue Addoperation:op];

}

iOS Development nsoperation & Nsoperationqueue

Related Article

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.