貓貓學iOS(五十三)多線程網路之NSOperation簡介

來源:互聯網
上載者:User

標籤:nsoperation   網路   多線程   ios   編程   

貓貓分享,必須精品

原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents

一:簡單介紹1:NSOperation的作?使用步驟:

配合使用NSOperation和NSOperationQueue也能實現多線程編程。

NSOperation和NSOperationQueue實現多線程的具體步驟:

(1)先將需要執行的操作封裝到一個NSOperation對象中(子類執行個體)。

NSOperationQueue的作用:NSOperation可以調用start方法來執行任務,但預設是同步執行的如果將NSOperation添加到NSOperationQueue(操作隊列)中,系統會自動非同步執行NSOperation中的操作。

(2)然後將NSOperation對象添加到NSOperationQueue中。

//這裡是兩個子類的方法後面詳細介紹- (void)addOperation:(NSOperation *)op;- (void)addOperationWithBlock:(void (^)(void))block;

(3)系統會?動將NSOperationQueue中的NSOperation取出來。

(4)將取出的NSOperation封裝的操作放到?條新線程中執?。

2.NSOperation的子類

NSOperation是個抽象類別,並不具備封裝操作的能力,必須使?它的子類

使用NSOperation?類的方式有3種:

(1)NSInvocationOperation

(2)NSBlockOperation

(3)自訂子類繼承NSOperation,實現內部相應的?法

NSOperation的三種用法(子類)1:NSInvocationOperation

最簡單的用法(在主線程中:)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1.建立1個操作    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];    // 2.開啟操作    [operation start];}-(void)download{    NSLog(@"download---%@", [NSThread currentThread]);}

非同步方法(記住這個)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1.建立一個隊列(非主隊列)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    // 2.建立1個操作    NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download) object:nil];    // 3 .將操作添加到隊列(非主隊列)————這一步內部相當於開啟    [queue addOperation:operation];}-(void)download{    NSLog(@"download---%@", [NSThread currentThread]);}

效果:線程3(其中系統會自動給我們分配最合適的線程)

注意:操作對象預設在主線程中執行(前一個),只有添加到隊列中才會開啟新的線程。即預設情況下,如果操作沒有放到隊列中queue中,都是同步執行。只有將NSOperation放到一個NSOperationQueue中,才會非同步執行操作

2:NSBlockOperation

第二種也很簡單:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1.建立一個隊列(非主隊列)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    // 2.建立1個操作    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{        NSLog(@"download---%@", [NSThread currentThread]);    }];    // 3 .將操作添加到隊列(非主隊列)————這一步內部相當於開啟    [queue addOperation:operation];}

效果:

這裡還有中寫法是直接將block添加到queue中

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1.建立一個隊列(非主隊列)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    // 2.將操作添加到隊列(非主隊列)————這一步內部相當於開啟    [queue addOperationWithBlock:^{        NSLog(@"download---%@", [NSThread currentThread]);    }];}
3:自訂子類繼承NSOperation,實現內部相應的?法

這個也不難,就是自訂一個NSOperation的子類,實現他的main方法

自訂類的寫法

////  NYOperation.m//  NSOperation使用////  Created by apple on 15-5-27.//  Copyright (c) 2015年 znycat. All rights reserved.//#import "NYOperation.h"@implementation NYOperation-(void)main{    NSLog(@"download---%@", [NSThread currentThread]);}@end

controller調用寫法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    // 1.建立一個隊列(非主隊列)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    // 2.建立1個操作    NYOperation *operation = [[NYOperation alloc]init];    // 3 .將操作添加到隊列(非主隊列)————這一步內部相當於開啟    [queue addOperation:operation];}

結果:

貓貓學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.