NSOperation
是一个抽象类,不能直接使用
NSInvocationOperation
(调用)NSBlockOperation
(块)NSOperationQueue
队列UIGestureRecognizer
CAAnimation
CAPRopertyAnimation
start
方法 会在当前线程执行 @selector
方法- (void)opDemo1 {
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:@"Invocation"];
// start方法 会在当前线程执行 @selector 方法
[op start];
}
- (void)downloadImage:(id)obj {
NSLog(@"%@ %@", [NSThread currentThread], obj);
}
异步执行
selector
方法- (void)opDemo2 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:@"queue"];
[q addOperation:op];
}
- (void)opDemo3 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
for (int i = 0; i < 10; ++i) {
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:@(i)];
[q addOperation:op];
}
}
执行效果:会开启多条线程,而且不是顺序执行。与GCD中并发队列&异步执行效果一样!
结论,在 NSOperation 中:
- (void)opDemo4 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@", [NSThread currentThread]);
}];
[q addOperation:op];
}
使用 block 来定义操作,所有的代码写在一起,更简单,便于维护!
- (void)opDemo5 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
for (int i = 0; i < 10; ++i) {
[q addOperationWithBlock:^{
NSLog(@"%@ %d", [NSThread currentThread], i);
}];
}
}
- (void)opDemo5 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
for (int i = 0; i < 10; ++i) {
[q addOperationWithBlock:^{
NSLog(@"%@ %d", [NSThread currentThread], i);
}];
}
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"block %@", [NSThread currentThread]);
}];
[q addOperation:op1];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:@"invocation"];
[q addOperation:op2];
}
NSOperationQueue
中添加任意 NSOperation
的子类- (void)opDemo6 {
NSOperationQueue *q = [[NSOperationQueue alloc] init];
[q addOperationWithBlock:^{
NSLog(@"耗时操作 %@", [NSThread currentThread]);
// 主线程更新 UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"更新 UI %@", [NSThread currentThread]);
}];
}];
}
/// 全局操作队列,统一管理所有的异步操作
@property (nonatomic, strong) NSOperationQueue *queue;
- (NSOperationQueue *)queue {
if (_queue == nil) {
_queue = [[NSOperationQueue alloc] init];
}
return _queue;
}
/// MARK: - 最大并发操作数
- (void)opDemo1 {
// 设置同时并发操作数
self.queue.maxConcurrentOperationCount = 2;
NSLog(@"start");
for (int i = 0; i < 10; ++i) {
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:1.0];
NSLog(@"%@ %d", [NSThread currentThread], i);
}];
[self.queue addOperation:op];
}
}
/// MARK: - 暂停 & 继续
- (IBAction)pauseAndResume {
if (self.queue.operationCount == 0) {
NSLog(@"没有操作");
return;
}
// 暂停或者继续
self.queue.suspended = !self.queue.isSuspended;
if (self.queue.isSuspended) {
NSLog(@"暂停 %tu", self.queue.operationCount);
} else {
NSLog(@"继续 %tu", self.queue.operationCount);
}
}
没有完成的操作,是包含在队列的操作数中的
/// MARK: - 取消所有操作
- (IBAction)cancelAll {
if (self.queue.operationCount == 0) {
NSLog(@"没有操作");
return;
}
// 取消对列中的所有操作,同样不会影响到正在执行中的操作!
[self.queue cancelAllOperations];
NSLog(@"取消全部操作 %tu", self.queue.operationCount);
}
/// MARK: - 依赖关系
- (void)dependency {
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"登录 %@", [NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"付费 %@", [NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"下载 %@", [NSThread currentThread]);
}];
NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"通知用户 %@", [NSThread currentThread]);
}];
[op2 addDependency:op1];
[op3 addDependency:op2];
[op4 addDependency:op3];
// 注意不要循环依赖
// [op1 addDependency:op4];
[self.queue addOperations:@[op1, op2, op3] waitUntilFinished:NO];
[[NSOperationQueue mainQueue] addOperation:op4];
NSLog(@"come here");
}