您的位置:首页 > 移动开发 > IOS开发

IOS学习 NSOperation 并发数量、暂停/继续、取消、等待、依赖 与GCD区别

2016-04-06 22:16 555 查看
@interface
ViewController ()

//定义一个全局的队列

@property (nonatomic,strong)NSOperationQueue
*opQueue;

@end

@implementation ViewController

//初始化对象

-(NSOperationQueue *)opQueue{

if (_opQueue ==
nil) {

_opQueue = [[NSOperationQueue
alloc]init];

}

return
_opQueue;

}

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

UIButton *btn = [[UIButton
alloc]initWithFrame:CGRectMake(100,
100,
100, 40)];

[btn setTitle:@"暂停/继续"
forState:UIControlStateNormal];

btn.backgroundColor = [UIColor
purpleColor];

[btn addTarget:self
action:@selector(btnAction)
forControlEvents:UIControlEventTouchUpInside];

[self.view
addSubview:btn];

UIButton *btnStop = [[UIButton
alloc]initWithFrame:CGRectMake(100,
200, 100,
40)];

[btnStop setTitle:@"取消全部"
forState:UIControlStateNormal];

btnStop.backgroundColor = [UIColor
purpleColor];

[btnStop addTarget:self
action:@selector(btnStop)
forControlEvents:UIControlEventTouchUpInside];

[self.view
addSubview:btnStop];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event{

[self
demo3];

}

-(void)demo1{

//创建队列:相当于GCD的并发(全局)队列

// NSOperationQueue *opq = [[NSOperationQueue alloc]init];

//主队列:与GCD主队列一致

NSOperationQueue *opq =[NSOperationQueue
mainQueue];

//创建多个操作:相当于GCD的异步执行的任务

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

NSOperation *op = [NSBlockOperation
blockOperationWithBlock:^{

NSLog(@"%@ %d",[NSThread
currentThread],i);

}];

[opq addOperation:op];

}

}

-(void)demo2{

NSOperationQueue *opq = [[NSOperationQueue
alloc]init];

//常用写法

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

[opq addOperationWithBlock:^{

NSLog(@"%@ %d",[NSThread
currentThread],i);

}];

}

//NSBlockOperation

NSBlockOperation *op1 = [NSBlockOperation
blockOperationWithBlock:^{

NSLog(@"op1 %@",[NSThread
currentThread]);}];

[op1 addExecutionBlock:^{

NSLog(@"op1-11111");

}];

// [opq addOperation:op1];

//NSInvocationOperation
不常用

NSOperation *op2 = [[NSInvocationOperation
alloc]initWithTarget:self
selector:@selector(task:)
object:@"Invocation"];
//多态的写法

// [opq addOperation:op2];

//依赖关系,可以跨队列,注意不要出现循环依赖

[op2 addDependency:op1];

//设置等待

[opq addOperations:@[op1,op2]
waitUntilFinished:YES];

NSLog(@"等待执行");

//线程间通信:在主线程里更新UI

NSBlockOperation *op3 = [NSBlockOperation
blockOperationWithBlock:^{

NSLog(@"更新UI......%@",[NSThread
currentThread]);}];

[[NSOperationQueue
mainQueue] addOperation:op3];

[op3 addDependency:op2];
//依赖关系,可以跨队列

}

-(void)task:(id)obj{

NSLog(@"op2 %@ %@",[NSThread
currentThread],obj);

}

-(void)demo3{

//设置最大并发数:不是线程的数量,而是同时执行操作的数量

self.opQueue.maxConcurrentOperationCount
= 2;

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

NSOperation *bp = [NSBlockOperation
blockOperationWithBlock:^{

[NSThread
sleepForTimeInterval:1];

NSLog(@"%@ %d",[NSThread
currentThread],i);

}];

[self.opQueue
addOperation:bp];

}

}

//挂起:暂停和继续

-(void)btnAction{

//判断操作数的数量

if (self.opQueue.operationCount
== 0) {

NSLog(@"没有操作");

return;

}

//暂停和继续:对队列的操作

self.opQueue.suspended = !self.opQueue.suspended;

if (self.opQueue.suspended)
{

NSLog(@"暂停");

}else{

NSLog(@"继续");

}

}

//取消队列里的所有操作,取消操作并不会影响队列的挂起状态

-(void)btnStop{

[self.opQueue
cancelAllOperations];

NSLog(@"取消全部");

//取消队列里的挂起状态

self.opQueue.suspended =
NO;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: