iOS多線程詳解(三)NSOperation

基于GCD的一層封裝,也是面向隊列和任務(wù)來開發(fā),比GCD更加面向?qū)ο?/h1>

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ǔ)充


![Snip20160723_3.png](http://upload-images.jianshu.io/upload_images/1777166-98ea5243b9ddabe4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容