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 in
186 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