標籤:
一:操作依賴和監聽
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController/** * 1:NSOperation的使用:1:先建立隊列NSOperationQueue:若不建立隊列直接封裝任務則預設在當前線程中串列執行任務,其隊列分為兩種主隊列和非主隊列,主隊列和GCD中的主隊列一樣[NSOperationQueue mainQueue],而alloc init 建立出來的隊列,預設具有串列和並發的功能,預設是並發的,可以通過指定最大並發數來確定其是串列隊列還是主隊列,為0,預設什麼都不做,為1預設為串列隊列,大於1為並發隊列 2:封裝任務:用NSOperation的子類,NSBlockOperation,NSInvercation,或是繼承NSOperation重寫main方法封裝任務,(還有快速封裝任務:1:[queue addOperationWithBlock:<#^(void)block#>]; 2:還可以在某個任務後追加任務:[op4 addExecutionBlock],當某個操作中的任務數量大於1後,會開啟子線程,其子線程有可能是開啟的非同步線程有可能是主線程還有可能是當前線程 3:還可以添加操作監聽:op3.completionBlock,當op3任務執行完畢後,就會進入此block執行任務 4:還可以實現操作依賴:可跨隊列依賴,但是不能循環相依性addDependency)3:將操作添加到隊列中,[queue addOperation:op1];此方法預設執行了[queue start]; * */-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1.建立隊列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; NSOperationQueue *queue2 = [[NSOperationQueue alloc]init]; //2.封裝操作 NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"1---%@",[NSThread currentThread]); }]; NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"2---%@",[NSThread currentThread]); }]; NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"3---%@",[NSThread currentThread]); }]; NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"4---%@",[NSThread currentThread]); }]; //操作監聽 op3.completionBlock = ^{ NSLog(@"++++客官,來看我吧------%@",[NSThread currentThread]); }; [op4 addExecutionBlock:^{ NSLog(@"5---%@",[NSThread currentThread]); }]; [op4 addExecutionBlock:^{ NSLog(@"6---%@",[NSThread currentThread]); }]; [op4 addExecutionBlock:^{ NSLog(@"7---%@",[NSThread currentThread]); }]; //添加操作依賴 //注意點:不能循環相依性 //可以跨隊列依賴 [op1 addDependency:op4];// [op4 addDependency:op1]; [op2 addDependency:op3]; //添加操作到隊列 [queue addOperation:op1]; [queue addOperation:op2]; [queue addOperation:op3]; [queue2 addOperation:op4]; }@end
二:線程間通訊
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self comBie];}-(void)download{ //http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e //1.開子線程下載圖片 //1.1 非主隊列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //1.2 封裝操作 NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"]; NSData *imageData = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); //3.更新UI [[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.imageView.image = image; NSLog(@"UI---%@",[NSThread currentThread]); }]; }]; //2.添加操作到隊列 [queue addOperation:download];}/* 1.下載圖片1 2.下載圖片2 3.合并圖片 */-(void)comBie{ //1.建立隊列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; __block UIImage *image1; __block UIImage *image2; //2 封裝操作,下載圖片1 NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"]; NSData *imageData = [NSData dataWithContentsOfURL:url]; image1 = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); }]; //3 封裝操作,下載圖片2 NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{ NSURL *url = [NSURL URLWithString:@"http://www.027art.com/feizhuliu/UploadFiles_6650/201109/2011091718442835.jpg"]; NSData *imageData = [NSData dataWithContentsOfURL:url]; image2 = [UIImage imageWithData:imageData]; NSLog(@"download---%@",[NSThread currentThread]); }]; //4.封裝合并圖片的操作 NSBlockOperation *combie = [NSBlockOperation blockOperationWithBlock:^{ //4.1 開上下文 UIGraphicsBeginImageContext(CGSizeMake(200, 200)); //4.2 畫圖1 [image1 drawInRect:CGRectMake(0, 0, 100, 200)]; //4.3 畫圖2 [image2 drawInRect:CGRectMake(100, 0, 100, 200)]; //4.4 根據上下文得到圖片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //4.5 關閉上下文 UIGraphicsEndImageContext(); //7.更新UI [[NSOperationQueue mainQueue] addOperationWithBlock:^{ self.imageView.image = image; NSLog(@"UI----%@",[NSThread currentThread]); }]; }]; //5.設定依賴關係 [combie addDependency:download1]; [combie addDependency:download2]; //6.添加操作到隊列中 [queue addOperation:download2]; [queue addOperation:download1]; [queue addOperation:combie];}@end
- 2.4 NSOperation實現線程間通訊
(1)開子線程下載圖片
```objc
//1.建立隊列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.使用簡便方法封裝操作並添加到隊列中
[queue addOperationWithBlock:^{
//3.在該block中下載圖片
NSURL *url = [NSURL URLWithString:@"http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"下載圖片操作--%@",[NSThread currentThread]);
//4.回到主線程重新整理UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"重新整理UI操作---%@",[NSThread currentThread]);
}];
}];
```
iOS開發NSOperation 三:操作依賴和監聽以及線程間通訊