Step 7 nsoperation (Advanced/GCD contrast)

Source: Internet
Author: User

Advanced Walkthroughs

Global queue

Global operations queue, unified management of all asynchronous operations

@property (nonatomic, strong) Nsoperationqueue *queue;

-(Nsoperationqueue *) Queue {

if (_queue = = nil) {

_queue = [[Nsoperationqueue alloc] init];

}

return _queue;

}

Maximum number of concurrent operations

MARK:-Maximum concurrent operations

-(void) OpDemo1 {

Set concurrent simultaneous operands

Self.queue.maxConcurrentOperationCount = 2;

NSLog (@ "start");

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

Nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{

[Nsthread sleepfortimeinterval:1.0];

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

}];

[Self.queue Addoperation:op];

}

}

Pause & continue to

MARK:-Pause & Resume

-(ibaction) Pauseandresume {

if (Self.queue.operationCount = = 0) {

NSLog (@ "no action");

Return

}

Pause or Resume

self.queue.suspended =!self.queue.issuspended;

if (self.queue.isSuspended) {

NSLog (@ "Pause%tu", self.queue.operationCount);

} else {

NSLog (@ "Continue%tu", self.queue.operationCount);

}

}

    • Queue pending, currently "not completed", is included in the operand of the queue
    • The queue hangs without affecting the execution state of the action already performed
    • Once the column is suspended, the added action is not dispatched

Cancel all operations

MARK:-Cancel all operations

-(ibaction) Cancelall {

if (Self.queue.operationCount = = 0) {

NSLog (@ "no action");

Return

}

Canceling all operations on a column also does not affect the operation in progress!

[Self.queue cancelalloperations];

NSLog (@ "Cancel all operations%tu", Self.queue.operationCount);

}

1 canceling all operations in the queue

2 Do not cancel operation in progress

3 does not affect the pending state of the queue

Dependent relationships

MARK:-Dependencies

-(void) Dependency {

Nsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP2 = [Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP3 = [Nsblockoperation blockoperationwithblock:^{

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

}];

Nsblockoperation *OP4 = [Nsblockoperation blockoperationwithblock:^{

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

}];

[Op2 ADDDEPENDENCY:OP1];

[Op3 ADDDEPENDENCY:OP2];

[OP4 ADDDEPENDENCY:OP3];

Be careful not to loop dependencies

[Op1 ADDDEPENDENCY:OP4];

[Self.queue Addoperations:@[op1, OP2, OP3] waituntilfinished:no];

[[Nsoperationqueue Mainqueue] ADDOPERATION:OP4];

NSLog (@ "come here");

}

with the GCD the contrast
    • GCD

Add a task (block) to the queue (Serial/concurrency/home column) and specify the function that the task executes (synchronous/asynchronous)

GCD is the underlying C-language API

IOS 4.0, concurrency technology for multicore processors

Executing in the queue is a block-composed task, which is a lightweight data structure

To stop a block that has joined the queue, you need to write complex code

dependencies between tasks need to be set through Barrier or synchronization tasks

You can only set the priority of a queue

Advanced Features:

Disposable Once

Deferred operation after

Dispatch Group

    • Nsoperation

Core concept: Adding operations (Async) to queues (global concurrent queues)

OC Framework, more object-oriented, is the encapsulation of GCD

IOS 2.0 launched, after Apple introduced GCD, the bottom of nsoperation all rewritten

Operation as an object that gives us more choices

You can cancel any tasks that have been set to be executed at any time, except for those that have been executed

Dependencies for operations can be set across queues

You can set the priority of each operation in a queue

Advanced Features:

Maximum number of operation concurrency (GCD not good to do)

Resume/Pause/Cancel All

To set dependencies on operations across queues

Step 7 nsoperation (Advanced/GCD comparison)

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.