iOS Development-Nsoperation advanced Features

Source: Internet
Author: User
Tags gcd

Directly on the code

Example 1:

@interface Tbviewcontroller ()

Queue

@property (Nonatomic,strong) Nsoperationqueue *queue;

@end

Pause and resume of queue (drag two buttons in storyboard, pause, continue, connect, respectively)

Time out

-(ibaction) Pause

{

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

NSLog (@ "no operation");

Return

}

Suspend pause is a queue, so the queue temporarily no longer dispatch tasks

NSLog (@ "pause");

[Self.queue Setsuspended:yes];

}

Go on

-(Ibaction) Resumue

{

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

NSLog (@ "no operation");

Return

}

NSLog (@ "continue");

[Self.queue Setsuspended:no];

}

Lazy Loading

-(Nsoperationqueue *) queue

{

if (_queue = = nil) {

_queue = [[Nsoperationqueue alloc]init];

}

return _queue;

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

[Self opDemo1];

}

Simultaneous number of concurrent threads

-(void) OpDemo1

{

Thread-open queues are determined by the GCD, and programmers cannot participate in

/*

The benefit of setting the maximum number of concurrent threads: thread-on consumption

3g:2~3 threads, fewer threads, less traffic, saving money, saving power

Wifi:5~6 threads, many threads, high efficiency, large flow, no money, can be recharged at any time

*/

Self.queue Maxconcurrentoperationcount = 3;//Maximum concurrent quantity

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

[Self.queue addoperationwithblock:^{

Blocking

[Nsthread Sleepfortimeinterval:2];

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

}];

}

}

Example 2: Operation Dependency-the order of execution of operations

-(void) OpDemo3

{

/*

For example:

1, download the small movie compression package

2. Decompression

3. Save the file

4. Notify the user

*/

NSBLOCKOPERATION*OP1 = [Nsblockoperation blockoperationwithblock:^{

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

}];

NSBLOCKOPERATION*OP2 = [Nsblockoperation blockoperationwithblock:^{

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

}];

NSBLOCKOPERATION*OP3 = [Nsblockoperation blockoperationwithblock:^{

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

}];

NSBLOCKOPERATION*OP4 = [Nsblockoperation blockoperationwithblock:^{

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

}];

Depend on

[Op2 ADDDEPENDENCY:OP1];

[Op3 ADDDEPENDENCY:OP3];

[OP4 ADDDEPENDENCY:OP3];

Cyclic dependency (must not appear)

[Op1 ADDDEPENDENCY:OP4];

  

Asynchronous waituntilfinished waits for all operations to complete before continuing

[Self.queue ADDOPERATIONS:@[OP1,OP2,OP3] waituntilfinished:no];

Primary queue

[[Nsoperationqueue Mainqueue] ADDOPERATION:OP4];

}

Summarize:

/*

Nsoperation

1. Is the package for the GCD

2, is OC, object-oriented, easy to use

3, provides some special methods, is GCD not easy to implement, for example: the maximum number of concurrent threads, pause and continue

4, you can specify the dependencies of the operation, the dependencies can be cross-queue

5, custom queue, is a concurrent queue, operation is an asynchronous task

GCD

1, is the C language, the difficulty is a bit big

2, once

3. After

4, GCD and a lot of complex functions

*/

iOS Development-Nsoperation advanced Features

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.