- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 120, 100);
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"NSOperation" forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(clickOperation) forControlEvents:UIControlEventTouchUpInside];
UIButton *blockButton = [UIButton buttonWithType:UIButtonTypeSystem];
blockButton.frame = CGRectMake(100, 220, 120, 100);
blockButton.backgroundColor = [UIColor blueColor];
[blockButton setTitle:@"BlockOperation" forState:UIControlStateNormal];
[blockButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[self.view addSubview:blockButton];
[blockButton addTarget:self action:@selector(clickBlock) forControlEvents:UIControlEventTouchUpInside];
UIButton *queueButton = [UIButton buttonWithType:UIButtonTypeSystem];
queueButton.frame = CGRectMake(100, 340, 120, 100);
queueButton.backgroundColor = [UIColor blueColor];
[queueButton setTitle:@"QueueOperation" forState:UIControlStateNormal];
[queueButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[self.view addSubview:queueButton];
[queueButton addTarget:self action:@selector(clickQueue) forControlEvents:UIControlEventTouchUpInside];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(50, 50, 50, 50);
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}
- (void)clickOperation {
//NSOperation 是一个抽象类,它不具备实际的开辟线程的作用,将NSOperation放在哪个线程中, 它就再哪个线程中执行回调方法
//这是NSOperation的一个子类,开辟子线程 三个参数的具体作用和NSThread alloc中的三个参数功能相同
NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:@"nil"];
//执行invocationOperation的回调方法
[invocationOperation start];
}
-(void)test {
NSLog(@"当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
}
- (void)clickBlock {
//NSOperation的第二个子类,实现方式是通过block
NSBlockOperation *blockOpe = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
}];
// 开始去执行
[blockOpe start];
}
- (void)clickQueue {
//operationQueue队列,将operation加入到队列中之后,operation会自动在子线程中执行,并且是并发执行。不用在手动调用start方法,系统会调用
//在operationQueue队列中,执行是无序的。
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *ope1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
}];
NSBlockOperation *ope2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"。。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
}];
NSBlockOperation *ope3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"。。。当前线程是:%@, 是否主线程:%d",[NSThread currentThread] ,[[NSThread currentThread] isMainThread]);
}];
//当我们想要队列中的执行时是有序的,就得给operation添加依赖关系,添加完依赖关系之后,先执行的是参数的operation。再执行调用依赖方法的operation,例如 [ope1 addDependency:ope2] 就先执行 ope2再执行ope1
[ope1 addDependency:ope2];
[ope2 addDependency:ope3];
[queue addOperation:ope1];
[queue addOperation:ope2];
[queue addOperation:ope3];
}
- (void)back {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end