iOS 多線程學習筆記 —— NSOperation

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   java   

本文複製、參考自文章:iOS多線程編程之NSOperation和NSOperationQueue的使用 ,主要為了加強個人對知識的理解和記憶,不做他用。原作者聲明:

著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝!

這裡對原作者的辛勤工作表示感謝!

1. 使用抽象的 NSOperation 的方式有兩種:

(1)用庫定義好的子類: NSInvocationOperation 和 NSBlockOperation。

(2)自訂類,繼承自NSOperation。和Java的Runnable一樣,NSOperation也是設計用來擴充的,只需重寫NSOperation的main方法,相當於Java中的Run方法。然後把NSOperation子類的對象放到NSOperationQueue隊列中,該隊列就會啟用並開始處理它。

2. 使用NSInvocationOperation的例子:非同步載入圖片

 (1) 在ViewController.m中添加多線程調用:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage:) object:kURL];    NSOperationQueue* queue = [[NSOperationQueue alloc]init];    [queue addOperation:operation];}

其他與NSThread樣本中的一致。

3. 使用自訂類的例子

將2.中的NSInvocationOperation類替換為自訂的NSOperation子類,在子類中的main方法中實現downloadImage方法即可。 

iOS 多線程學習筆記 —— NSOperation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.