基于GCD的一層封裝,也是面向隊列和任務(wù)來開發(fā),比GCD更加面向?qū)ο?/h1>
NSOperation是個抽象類,并不具備封裝操作的能力,必須使用它的子類
- 使用NSOperation子類的方式有3種NSInvocationOperation
- NSBlockOperation
- 自定義子類繼承NSOperation,實現(xiàn)內(nèi)部相應(yīng)的方法
NSOperation是個抽象類,并不具備封裝操作的能力,必須使用它的子類
- 使用NSOperation子類的方式有3種NSInvocationOperation
- NSBlockOperation
- 自定義子類繼承NSOperation,實現(xiàn)內(nèi)部相應(yīng)的方法
注意:NSOperation可以調(diào)用start方法來執(zhí)行任務(wù),但默認(rèn)是同步執(zhí)行的
如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統(tǒng)會自動異步執(zhí)行NSOperation中的操作
GCD 和 NSOperation 的隊列對比
GCD的隊列類型
-
并發(fā)隊列
- 自己創(chuàng)建的
- 全局
-
串行隊列
- 主隊列
- 自己創(chuàng)建的
// 1.獲得全局的并發(fā)隊列 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2.將任務(wù)加入隊列 dispatch_async(queue, ^{ for (NSInteger i = 0; i<10; i++) { NSLog(@"1-----%@", [NSThread currentThread]); } }); // 將任務(wù)加入隊列 dispatch_async(queue, ^{ for (NSInteger i = 0; i<10; i++) { NSLog(@"2-----%@", [NSThread currentThread]); } }); // 將任務(wù)加入隊列 dispatch_async(queue, ^{ for (NSInteger i = 0; i<10; i++) { NSLog(@"3-----%@", [NSThread currentThread]); } });
>#### NSOperationQueue的隊列類型
- 主隊列
- [NSOperationQueue mainQueue]
- 凡是添加到主隊列中的任務(wù)(NSOperation),都會放到主線程中執(zhí)行
- 非主隊列(其他隊列)
- [[NSOperationQueue alloc] init]
- 同時包含了:串行、并發(fā)功能
- 添加到這種隊列中的任務(wù)(NSOperation),就會自動放到子線程中執(zhí)行
// 創(chuàng)建隊列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 設(shè)置最大并發(fā)操作數(shù)
queue.maxConcurrentOperationCount = 2; 并行隊列
// queue.maxConcurrentOperationCount = 1; // 就變成了串行隊列
// 添加操作到隊列中
// [queue addOperation:op1];
[queue addOperationWithBlock:^{
NSLog(@"download1 --- %@", [NSThread currentThread]);
}];
[queue addOperationWithBlock:^{
NSLog(@"download2 --- %@", [NSThread currentThread]);
}];
##線程間通信
-1.簡單例子( 和GCD很像)
[[[NSOperationQueue alloc] init] addOperationWithBlock:^{
// 圖片的網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
// 加載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成圖片
UIImage *image = [UIImage imageWithData:data];
// 回到主線程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
}];
}];
}
-2.簡單模擬下載圖片并合成圖片(添加依賴關(guān)系,控制線程的執(zhí)行順序)
__block UIImage *image1 = nil;
// 下載圖片1
NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
// 圖片的網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
// 加載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成圖片
image1 = [UIImage imageWithData:data];
}];
__block UIImage *image2 = nil;
// 下載圖片2
NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
// 圖片的網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
// 加載圖片
NSData *data = [NSData dataWithContentsOfURL:url];
// 生成圖片
image2 = [UIImage imageWithData:data];
}];
// 合成圖片
NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{
// 開啟新的圖形上下文
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
// 繪制圖片
[image1 drawInRect:CGRectMake(0, 0, 50, 100)];
image1 = nil;
[image2 drawInRect:CGRectMake(50, 0, 50, 100)];
image2 = nil;
// 取得上下文中的圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
// 回到主線程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
}];
}];
[combine addDependency:download1];
[combine addDependency:download2];
[queue addOperation:download1];
[queue addOperation:download2];
[queue addOperation:combine];
- 補(bǔ)充
