NSOperation
2023-09-14 08:57:59 时间
前面了解了GCD也比较详细,所以Thread和NSOperation大概了解一下。参考:http://blog.csdn.net/crycheng/article/details/21799611
还是上代码
// // ViewController.m // Operation // Created by City--Online on 15/4/1. // Copyright (c) 2015年 City--Online. All rights reserved. #import "ViewController.h" @interface ViewController () UIButton *btn; NSBlockOperation *blockoperation; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; btn=[UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:@"按钮" forState:UIControlStateNormal]; btn.backgroundColor=[UIColor redColor]; btn.frame=CGRectMake(30, 30, 50, 50); [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(threadfun) object:nil]; NSOperationQueue *operationqueue=[[NSOperationQueue alloc]init]; [operationqueue addOperation:operation]; blockoperation=[NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockoperation"); -(void)btnclick:(id)sender //增加block到线程中 [blockoperation addExecutionBlock:^{ NSLog(@"addExecutionBlock"); [blockoperation setCompletionBlock:^{ NSLog(@"setCompletionBlock"); [blockoperation start];//不能放在addExecutionBlock之前start -(void)threadfun NSLog(@"threadfun"); //更新主线程 [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO]; -(void)updateUI NSLog(@"uodateUI"); [btn setTitle:@"123" forState:UIControlStateNormal]; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. @end
配合使用 NSOperation 和 NSOperationQueue 也能实现多线程编程; NSOperation 和 NSOperationQueue 实现多线程的具体步骤 先将需要执行的操作封装到一个 NS...
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.