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;
}
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;
}
相关文章推荐
- iOS wkwebkit 播放HTML5 视频 全屏问题解决
- iOS_主要的函数
- iOS设计中不同屏幕适配的方法-登陆界面
- iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
- iOS中WebSocket的使用
- iOS之旅-越过坑点
- ios定位
- IOS openGL es 学习一
- iOS——程序启动过程
- 在ios中自定义tabbar跳转隐藏问题
- iOS 单独设置某个控制器横屏 —— HERO博客
- IOS程序开发概览
- iOS 开发技巧 更新
- iOS tableView 静态单元格的实现
- 程序猿们,iOS中是有生命周期的
- Method Swizzle黑魔法,修改 ios 系统类库方法 SEL IMP
- iOS 拨打电话 发送短信
- iOS 沙盒机制
- iOS 国际化语言
- iOS 通过 JSPatch 实时修复线上 bug!