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