标签:nsoperatio
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");
}
GCD
任务(block)
添加到队列(串行/并发/主队列),并且指定任务执行的函数(同步/异步)block
构成的任务,这是一个轻量级的数据结构queue
的 block
需要写复杂的代码Barrier
或者同步任务设置任务之间的依赖关系NSOperation
操作(异步)
添加到队列(全局的并发队列)
Operation
作为一个对象,为我们提供了更多的选择DownloadImageOperation
继承自 NSOperation// 实例化自定义操作
DownloadImageOperation *op = [[DownloadImageOperation alloc] init];
// 将自定义操作添加到下载队列
[self.downloadQueue addOperation:op];
对于自定义操作,只要重写了
main
方法,当队列调度操作执行时,会自动运行main
方法
注意:main
方法中需要使用自动释放池!
- (void)main {
@autoreleasepool {
NSLog(@"%@", [NSThread currentThread]);
}
}
/// 要下载图像的 URL 字符串
@property (nonatomic, copy) NSString *URLString;
// 实例化自定义操作
DownloadImageOperation *op = [[DownloadImageOperation alloc] init];
// 设置操作属性
op.URLString = @"https://www.baidu.com/img/bdlogo.png";
// 将自定义操作添加到下载队列,操作启动后会执行 main 方法
[self.downloadQueue addOperation:op];
注意,
main
方法被调用时,属性已经准备就绪
CompletionBlock
属性// 设置完成回调
[op setCompletionBlock:^{
NSLog(@"完成 %@", [NSThread currentThread]);
}];
CompletionBlock
,当操作执行完毕后,就会被自动调用CompletionBlock
既不在主线程也不在操作执行所在线程CompletionBlock
无法传递参数/// 完成回调 Block
@property (nonatomic, copy) void (^finishedBlock)(UIImage *image);
// 设置自定义完成回调
[op setFinishedBlock:^(UIImage *image) {
NSLog(@"finished %@ %@", [NSThread currentThread], image);
}];
// 判断自定义回调是否存在
if (self.finishedBlock != nil) {
// 通常为了简化调用方的代码,异步操作结束后的回调,大多在主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.finishedBlock(@"hello");
}];
}
类
方法/// 实例化下载图像操作
///
/// @param URLString 图像 URL 字符串
/// @param finished 完成回调 Block
///
/// @return 下载操作实例
+ (instancetype)downloadImageOperationWithURLString:(NSString *)URLString finished:(void (^)(UIImage *image))finished;
+ (instancetype)downloadImageOperationWithURLString:(NSString *)URLString finished:(void (^)(UIImage *))finished {
DownloadImageOperation *op = [[DownloadImageOperation alloc] init];
op.URLString = URLString;
op.finishedBlock = finished;
return op;
}
// 使用类方法实例化下载操作
DownloadImageOperation *op = [DownloadImageOperation downloadImageOperationWithURLString:@"http://www.baidu.com/img/bdlogo.png" finished:^(UIImage *image) {
NSLog(@"%@", image);
}];
// 将自定义操作添加到下载队列,操作启动后会执行 main 方法
[self.downloadQueue addOperation:op];
在关键节点添加
isCancelled
判断
for (int i = 0; i < 10; ++i) {
NSString *urlString = [NSString stringWithFormat:@"http://www.xxx.com/%04d.png", i];
DownloadImageOperation *op = [DownloadImageOperation downloadImageOperationWithURLString:urlString finished:^(UIImage *image) {
NSLog(@"===> %@", image);
}];
// 将自定义操作添加到下载队列,操作启动后会执行 main 方法
[self.downloadQueue addOperation:op];
}
_downloadQueue.maxConcurrentOperationCount = 2;
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
[self.downloadQueue cancelAllOperations];
}
cancelAllOperations
会向队列中的所有操作发送Cancel
消息
main
方法,在关键节点判断- (void)main {
NSLog(@"%s", __FUNCTION__);
@autoreleasepool {
NSLog(@"下载图像 %@", self.URLString);
// 模拟延时
[NSThread sleepForTimeInterval:1.0];
if (self.isCancelled) {
NSLog(@"1.--- 返回");
return;
}
// 判断自定义回调是否存在
if (self.finishedBlock != nil) {
// 通常为了简化调用方的代码,异步操作结束后的回调,大多在主线程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.finishedBlock(self.URLString);
}];
}
}
}
- (void)start {
[super start];
NSLog(@"%s", __FUNCTION__);
}
注意:如果操作状态已经是
Cancel
,则不会执行main
函数
start
方法将线程放入可调度线程池
main
方法标签:nsoperatio
原文地址:http://blog.csdn.net/jiahao8915/article/details/47691107