多线程之NSOperation小结

多线程之NSOperation小结,第1张

概述一、NSOperation 抽象类 NSOperation 是一个"抽象类",不能直接使用。抽象类的用处是定义子类共有的属性和方法。 NSOperation 是基于 GC

一、NSOperation 抽象类

NSOperation 是一个"抽象类",不能直接使用。抽象类的用处是定义子类共有的属性和方法。NSOperation 是基于 GCD 做的面向对象的封装。相比较 GCD 使用更加简单,并且提供了一些用 GCD 不是很好实现的功能。苹果公司推荐使用的并发技术。两个子类:NSInvocationoperation (调用)NSBlockOperation (块)

相比NSInvocationoperation推荐使用NSBlockOperation,代码简单,同时由于闭包性使它没有传参问题。

NSOperationQueue 队列

 

已经学习过的抽象类

UIGestureRecognizerCAAnimationCAPropertyAnimation

 

二、 NSOperation 和 GCD 的核心概念

GCD的核心概念:将 任务(block) 添加到队列,并且指定执行任务的函数。NSOperation 的核心概念:将 *** 作 添加到 队列。

三、NSOperation 和 GCD的区别:

GCD

将任务(block)添加到队列(串行/并发/主队列),并且指定任务执行的函数(同步/异步)GCD是底层的C语言构成的APIiOS 4.0 推出的,针对多核处理器的并发技术在队列中执行的是由 block 构成的任务,这是一个轻量级的数据结构要停止已经加入 queue 的 block 需要写复杂的代码需要通过 barrIEr 或者同步任务设置任务之间的依赖关系只能设置队列的优先级高级功能:一次性 once延迟 *** 作 after调度组

NSOperation

核心概念:把 *** 作(异步)添加到队列。OC 框架,更加面向对象,是对 GCD 的封装。iOS 2.0 推出的,苹果推出 GCD 之后,对 NSOperation 的底层全部重写。Operation作为一个对象,为我们提供了更多的选择。可以跨队列设置 *** 作的依赖关系可以设置队列中每一个 *** 作的优先级高级功能:最大 *** 作并发数(GCD不好做)继续/暂停/全部取消跨队列设置 *** 作的依赖关系

四、代码实践

  1 //  2 //  VIEwController.m  3   NSOperationTest  4   5   Created by mayl on 2018/1/5.  6   copyright © 2018年. All rights reserved.  7 //  8   9 #import "VIEwController.h" 10  11 @interface VIEwController () 12 @property(nonatomic,strong) NSOperationQueue *gOpQueue; 13 @end 14  15 @implementation VIEwController 16  17 - (voID)vIEwDIDLoad { 18     [super vIEwDIDLoad]; 19     [self setUpUI]; 20      21     [self aysncCon]; 22     [self maxConCount]; 23     [self oftenUse]; 24     [self setUpDependence]; 25     [self waitUntilFinished]; 26 } 27  28 - ()setUpUI{ 29      30     暂停,继续按钮 31     UIbutton *lBtn4Pause = [UIbutton buttonWithType:UIbuttonTypeCustom]; 32     [self.vIEw addSubvIEw:lBtn4Pause]; 33      34     lBtn4Pause.frame = CGRectMake(10,100,1)">50); 35     [lBtn4Pause setTitle:@"挂起" forState:UIControlStatenormal]; 36     lBtn4Pause.TitleLabel.numberOflines = 0; 37     [lBtn4Pause setTitlecolor:[UIcolor blackcolor] forState:UIControlStatenormal]; 38     [lBtn4Pause addTarget:self action:@selector(pauseBtnDIDClick:) forControlEvents:UIControlEventtouchUpInsIDe]; 39      40     取消所有任务按钮 41     UIbutton *lBtn4CancelAll = 42     [self.vIEw addSubvIEw:lBtn4CancelAll]; 43      44     lBtn4CancelAll.frame = CGRectMake(150,1)"> 45     [lBtn4CancelAll setTitle:cancel all 46     [lBtn4CancelAll setTitlecolor:[UIcolor redcolor] forState:UIControlStatenormal]; 47     [lBtn4CancelAll addTarget:self action:@selector(cancelAllBtnDIDClick:) forControlEvents:UIControlEventtouchUpInsIDe]; 48  49  50 #pragma mark -  action 51  52 /** 53  队列挂起,当前"没有完成的 *** 作",是包含在队列的 *** 作数中的。 54  队列挂起,不会影响已经执行 *** 作的执行状态。 55  队列一旦被挂起,再添加的 *** 作不会被调度。 56  */ 57 - (voID)pauseBtnDIDClick:(UIbutton *)btn{ 58     NSLog(队列中 *** 作数:%zd,self.gOpQueue.operationCount); 59     if (0 == self.gOpQueue.operationCount) { 60         NSLog(队列中无 *** 作 61         return 62     } 63      64     NSLog(3:%d 65     self.gOpQueue.suspended = !self.gOpQueue.isSuspended; 66     NSLog(4:%d 67     if (self.gOpQueue.isSuspended) { 68         NSLog(队列挂起 69         [btn setTitle:继续 70              forState:UIControlStatenormal]; 71     }else{ 72         NSLog(队列继续 73         [btn setTitle: 74  75  76  77  78  79  取消队列中所有的 *** 作。 80  不会取消正在执行中的 *** 作。 81  不会影响队列的挂起状态 82   83 - (voID)cancelAllBtnDIDClick:(UIbutton * 84      85         NSLog( 86          87  88      89     NSLog(取消队列中所有 *** 作,此方法不会改变队列挂起状态 90     [self.gOpQueue cancelAllOperations]; 91      92     NSLog(1:%d 93     self.gOpQueue.suspended = ! 94     NSLog(2:%d 95  96  97 * 默认是:异步,并发  98 - ()aysncCon{ 99     NSOperationQueue *lQueue = [[NSOperationQueue alloc] init];100     for (int i = 0; i < 20; ++i) {101         [lQueue addOperationWithBlock:^102             [NSThread sleepForTimeInterval:1];103             NSLog(%d,%@104         }];105 106 107 108 * 最大并发数:The maximum number of queued operations that can execute at the same time.109 - ()maxConCount{110     NSOperationQueue *lQueue =111     lQueue.maxConcurrentoperationCount = 2112     30; ++113         [lQueue addOperationWithBlock:^114             [NSThread sleepForTimeInterval:115             NSLog(116 117 118     119     self.gOpQueue = lQueue;120 121 122 * 常用:子线程耗时,主线程更新UI 123 - ()oftenUse{124     NSOperationQueue *lQ =125     126     [lQ addOperationWithBlock:^127         NSLog(耗时 *** 作开始,1)">128         [NSThread sleepForTimeInterval:3129         NSLog(耗时 *** 作结束130         131         [[NSOperationQueue mainQueue] addOperationWithBlock:^132             NSLog(主线程更新UI,%@133                   [NSThread currentThread]);134 135         136     }];137 138 139 * 设置依赖 140 - ()setUpDependence{141     NSOperationQueue *lQ =142     143     [lQ addOperationWithBlock:^144         [NSThread sleepForTimeInterval:145         NSLog(do something,1)">146               [NSThread currentThread]);147 148     149     NSBlockOperation *lOp1 = [NSBlockOperation blockOperationWithBlock:^150         [NSThread sleepForTimeInterval:151         NSLog(1:登录,1)">152 153 154     155     NSBlockOperation *lOp2 = [NSBlockOperation blockOperationWithBlock:^156         [NSThread sleepForTimeInterval:157         NSLog(2:购买点券,1)">158 159 160     161     NSBlockOperation *lOp3 = [NSBlockOperation blockOperationWithBlock:^162         [NSThread sleepForTimeInterval:163         NSLog(3:使用点券,1)">164 165 166     167     NSBlockOperation *lOp4 = [NSBlockOperation blockOperationWithBlock:^168         [NSThread sleepForTimeInterval:169         NSLog(4:返回结果,1)">170 171 172     173     [lOp2 addDependency:lOp1];174     [lOp3 addDependency:lOp2];175     [lOp4 addDependency:lOp3];176     177     下面加的话会循环依赖,导致任何 *** 作都无法进行,程序不会崩溃。178     [lOp1 addDependency:lOp4];179     180     [lQ addOperations:@[lOp4,lOp3] waitUntilFinished:NO];181     [lQ addOperations:@[lOp2,lOp1] waitUntilFinished:NO];182 183 184 *执行效果如下:185  2018-01-05 19:54:31.721539+0800 NSOperationTest[578:156322] come in186  2018-01-05 19:54:33.727691+0800 NSOperationTest[578:156342] 0:do others,<NSThread: 0x1c027f740>{number = 3,name = (null)}187  2018-01-05 19:54:34.731836+0800 NSOperationTest[578:156342] 1:登录,1)">188  2018-01-05 19:54:35.737375+0800 NSOperationTest[578:156342] 2:购买点券,1)">189  2018-01-05 19:54:36.742936+0800 NSOperationTest[578:156342] 3:使用点券,1)">190  2018-01-05 19:54:37.746491+0800 NSOperationTest[578:156342] 4:show Time,1)">191  2018-01-05 19:54:38.764408+0800 NSOperationTest[578:156341] 5:[lQ addOperations:@[lOp4,lOp3] waitUntilFinished:YES];实现了不设置依赖,且我需要最后执行,<NSThread: 0x1c04631c0>{number = 4,1)">192  193 - ()waitUntilFinished{194     NSOperationQueue *lQ =195     196     NSLog(come in197     NSBlockOperation *lOp0 = [NSBlockOperation blockOperationWithBlock:^198         [NSThread sleepForTimeInterval:199         NSLog(0:do others,1)">200 201 202     203     NSBlockOperation *lOp1 = [NSBlockOperation blockOperationWithBlock:^204         [NSThread sleepForTimeInterval:205         NSLog(206 207 208     209     NSBlockOperation *lOp2 = [NSBlockOperation blockOperationWithBlock:^210         [NSThread sleepForTimeInterval:211         NSLog(212 213 214     215     NSBlockOperation *lOp3 = [NSBlockOperation blockOperationWithBlock:^216         [NSThread sleepForTimeInterval:217         NSLog(218 219 220     221     NSBlockOperation *lOp4 = [NSBlockOperation blockOperationWithBlock:^222         [NSThread sleepForTimeInterval:223         NSLog(4:show Time,1)">224 225 226     227     NSBlockOperation *lOp5 = [NSBlockOperation blockOperationWithBlock:^228         [NSThread sleepForTimeInterval:229         230         NSLog(5:[lQ addOperations:@[lOp4,1)">231 232 233 234 235 236 237 238     执行顺序跟在数组中的顺序无关239     waitUntilFinished:If YES,the current thread is blocked until all of the specifIEd operations finish executing. If NO,the operations are added to the queue and control returns immediately to the caller.(If YES,当前线程会被阻塞,直到数组中所有 *** 作执行完毕。下局代码是直到lOp5执行完毕,才会执行后续 *** 作)240     [lQ addOperations:@[lOp0] waitUntilFinished:YES];241     242 243 244     245     [lQ addOperation:lOp5];246 247 248 @end

 

总结

以上是内存溢出为你收集整理的多线程之NSOperation小结全部内容,希望文章能够帮你解决多线程之NSOperation小结所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/999322.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存