标签:
是OC语言中基于GCD的面向对象的封装
使用起来比GCD更加简单(面向对象)
供了一些用GCD不好实现的功能
苹果推荐使用,使用NSOperation不用关心线程以及线程的生命周期
与GCD中的队列类似的概念
//创建操作
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:
@selector(downloadFile:) object:@"fileName"];
//在当前线程执行方法(开始执行操作)
[op start];
此方法不开线程
//创建操作
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector: @selector(downloadFile:) object:@"fileName"];
//将操作添加到队列,会自动异步调用方法
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:op];
- (void)downloadFile:(id)object{ NSLog(@"下载:%@----线程:%@",object,[NSThread currentThread]);
}
开线程,异步执行
- (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并发队列,异步执行
//队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (int i = 0; i < 10; i++) {
//操作
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"down %d %@",i,[NSThread currentThread]); }];
[queue addOperation:op]; }
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
for (int i = 0; i < 10; i++) {
[queue addOperationWithBlock:^{
NSLog(@"down %d %@",i,[NSThread currentThread]); }];
}
@property (nonatomic, strong) NSOperationQueue *queue; 懒加载队列
- (NSOperationQueue *)queue{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc] init]; }
return _queue; }
for (int i = 0; i < 10; i++) {
[self.queue addOperationWithBlock:^{
NSLog(@"down %d %@",i,[NSThread currentThread]); }];
}
[op1 setCompletionBlock:^{
NSLog(@"....."); }];
- (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];
}
}
GCD不能指定最大线程数
/// 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");
}
用来控制任务之间执行的顺序
一次性 once
延迟操作 after
调度组
最大操作并发数(GCD不好做)
继续/暂停/全部取消
跨队列设置操作的依赖关系
标签:
原文地址:http://blog.csdn.net/wangjunling888/article/details/51353636