#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self opDemo];
}
// NSOperation可以使用依赖
// GCD只能通过同步任务来完成
- (void)opDemo {
// 创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
// 需要操作对象
NSBlockOperation *op0 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"登录...");
}];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:2];
NSLog(@"扣费...");
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
// [NSThread sleepForTimeInterval:2];
NSLog(@"下载文件...");
}];
// 添加依赖 -- 添加依赖的时候要注册不要循环依赖
// 添加依赖的时候,条件要充分,
[op1 addDependency:op0];
[op2 addDependency:op1];
// 把所有的操作添加到队列
[queue addOperations:@[op0, op1, op2] waitUntilFinished:NO];
NSLog(@"end");
}
@end
多线程:NSOperation 的依赖使用
最新推荐文章于 2021-03-10 11:06:08 发布