国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

詳解iOS多線程應(yīng)用開發(fā)中使用NSOperation類的操作方法

2020-02-19 15:52:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

iOS開發(fā)中多線程的實(shí)現(xiàn)不僅可以通過(guò)nsthread實(shí)現(xiàn),還可以通過(guò)nsoperation實(shí)現(xiàn),而nsthread基于更高級(jí)別的GCD封裝,今天這篇文章是詳解iOS多線程應(yīng)用開發(fā)中使用NSOperation類的操作方法,一起跟著武林技術(shù)頻道小編來(lái)了解吧!

一、NSOperation簡(jiǎn)介

1.簡(jiǎn)單說(shuō)明

NSOperation的作?:配合使用NSOperation和NSOperationQueue也能實(shí)現(xiàn)多線程編程

NSOperation和NSOperationQueue實(shí)現(xiàn)多線程的具體步驟:

(1)先將需要執(zhí)行的操作封裝到一個(gè)NSOperation對(duì)象中

(2)然后將NSOperation對(duì)象添加到NSOperationQueue中

(3)系統(tǒng)會(huì)?動(dòng)將NSOperationQueue中的NSOperation取出來(lái)

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

?2.NSOperation的子類

NSOperation是個(gè)抽象類,并不具備封裝操作的能力,必須使?它的子類

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

(1)NSInvocationOperation

(2)NSBlockOperation

(3)自定義子類繼承NSOperation,實(shí)現(xiàn)內(nèi)部相應(yīng)的?法

二、 具體說(shuō)明

1.NSInvocationOperation子類

創(chuàng)建對(duì)象和執(zhí)行操作:

復(fù)制代碼 代碼如下:

//創(chuàng)建操作對(duì)象,封裝要執(zhí)行的任務(wù)
??? //NSInvocationOperation?? 封裝操作
??? NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
???
??? //執(zhí)行操作
??? [operation start];

?

說(shuō)明:一旦執(zhí)?操作,就會(huì)調(diào)用target的test方法

代碼示例:

復(fù)制代碼 代碼如下:

//
//? YYViewController.m
//? 01-NSOperation基本1
//
//? Created by 孔醫(yī)己 on 14-6-25.
//? Copyright (c) 2014年 itcast. All rights reserved.
//

?

#import "YYViewController.h"

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];
???
??? //NSOperation:抽象類,不具備封裝功能
???
??? //創(chuàng)建操作對(duì)象,封裝要執(zhí)行的任務(wù)
??? //NSInvocationOperation?? 封裝操作
??? NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil];
???
??? //執(zhí)行操作
??? [operation start];

}

-(void)test
{
???
??? NSLog(@"--test--%@--",[NSThread currentThread]);
}
@end


打印查看:

?

?

?

2015111391700829.png (797×26)

注意:操作對(duì)象默認(rèn)在主線程中執(zhí)行,只有添加到隊(duì)列中才會(huì)開啟新的線程。即默認(rèn)情況下,如果操作沒有放到隊(duì)列中queue中,都是同步執(zhí)行。只有將NSOperation放到一個(gè)NSOperationQueue中,才會(huì)異步執(zhí)行操作

2.NSBlockOperation子類

創(chuàng)建對(duì)象和添加操作:

復(fù)制代碼 代碼如下:

//創(chuàng)建NSBlockOperation操作對(duì)象
??? NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
??????? //......
??? }];
???
??? //添加操作
??? [operation addExecutionBlock:^{
??????? //....
??? }];


代碼示例:

?

代碼1:

復(fù)制代碼 代碼如下:

//
//? YYViewController.m
//? 02-NSTherad基本2
//
//? Created by 孔醫(yī)己 on 14-6-25.
//? Copyright (c) 2014年 itcast. All rights reserved.
//

?

#import "YYViewController.h"

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];
???
??? //創(chuàng)建NSBlockOperation操作對(duì)象
??? NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
??????? NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
??? }];
???
???
??? //開啟執(zhí)行操作
??? [operation start];
}
@end


打印查看:

?

?

?

2015111391736283.png (812×26)

代碼2:

復(fù)制代碼 代碼如下:

//
//? YYViewController.m
//? 02-NSTherad基本2
//
//? Created by 孔醫(yī)己 on 14-6-25.
//? Copyright (c) 2014年 itcast. All rights reserved.
//

?

#import "YYViewController.h"

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];
???
??? //創(chuàng)建NSBlockOperation操作對(duì)象
??? NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
??????? NSLog(@"NSBlockOperation------%@",[NSThread currentThread]);
??? }];
???
??? //添加操作
??? [operation addExecutionBlock:^{
??????? NSLog(@"NSBlockOperation1------%@",[NSThread currentThread]);
??? }];
???
??? [operation addExecutionBlock:^{
??????? NSLog(@"NSBlockOperation2------%@",[NSThread currentThread]);
??? }];
???
??? //開啟執(zhí)行操作
??? [operation start];
}
@end

?

?

?

2015111391842936.png (820×63)

注意:只要NSBlockOperation封裝的操作數(shù) > 1,就會(huì)異步執(zhí)行操作

?

3.NSOperationQueue

NSOperationQueue的作?:NSOperation可以調(diào)?start?法來(lái)執(zhí)?任務(wù),但默認(rèn)是同步執(zhí)行的

如果將NSOperation添加到NSOperationQueue(操作隊(duì)列)中,系統(tǒng)會(huì)自動(dòng)異步執(zhí)行NSOperation中的操作

添加操作到NSOperationQueue中,自動(dòng)執(zhí)行操作,自動(dòng)開啟線程

復(fù)制代碼 代碼如下:

//創(chuàng)建NSOperationQueue
??? NSOperationQueue * queue=[[NSOperationQueue alloc]init];
??? //把操作添加到隊(duì)列中
??? //第一種方式
??? [queue addOperation:operation1];
??? [queue addOperation:operation2];
??? [queue addOperation:operation3];
??? //第二種方式
??? [queue addOperationWithBlock:^{
??????? NSLog(@"NSBlockOperation3--4----%@",[NSThread currentThread]);
??? }];

?

復(fù)制代碼 代碼如下:

?


- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;


代碼示例:

?

復(fù)制代碼 代碼如下:

?


//
//? YYViewController.m
//? 03-NSOperation基本3
//
//? Created by 孔醫(yī)己 on 14-6-25.
//? Copyright (c) 2014年 itcast. All rights reserved.
//

?

#import "YYViewController.h"

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];

??? //創(chuàng)建NSInvocationOperation對(duì)象,封裝操作
??? NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
??? NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
??? //創(chuàng)建對(duì)象,封裝操作
??? NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
??????? NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
??? }];
??? [operation3 addExecutionBlock:^{
??????? NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
??? }];
???
??? //創(chuàng)建NSOperationQueue
??? NSOperationQueue * queue=[[NSOperationQueue alloc]init];
??? //把操作添加到隊(duì)列中
??? [queue addOperation:operation1];
??? [queue addOperation:operation2];
??? [queue addOperation:operation3];
}

?

復(fù)制代碼 代碼如下:

?


-(void)test1
{
??? NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
}

?

-(void)test2
{
??? NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
}

@end


打印效果:

?

?

?

2015111392029822.png (900×83)

注意:系統(tǒng)自動(dòng)將NSOperationqueue中的NSOperation對(duì)象取出,將其封裝的操作放到一條新的線程中執(zhí)行。上面的代碼示例中,一共有四個(gè)任務(wù),operation1和operation2分別有一個(gè)任務(wù),operation3有兩個(gè)任務(wù)。一共四個(gè)任務(wù),開啟了四條線程。通過(guò)任務(wù)執(zhí)行的時(shí)間全部都是273可以看出,這些任務(wù)是并行執(zhí)行的。

提示:隊(duì)列的取出是有順序的,與打印結(jié)果并不矛盾。這就好比,選手A,BC雖然起跑的順序是先A,后B,然后C,但是到達(dá)終點(diǎn)的順序卻不一定是A,B在前,C在后。
下面使用for循環(huán)打印,可以更明顯的看出任務(wù)是并發(fā)執(zhí)行的。

代碼示例:

復(fù)制代碼 代碼如下:

#import "YYViewController.h"

?

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];

??? //創(chuàng)建NSInvocationOperation對(duì)象,封裝操作
??? NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
??? NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
??? //創(chuàng)建對(duì)象,封裝操作
??? NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
??????? for (int i=0; i<5; i++) {
??????????? NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
??????? }
??? }];
??? [operation3 addExecutionBlock:^{
??????? for (int i=0; i<5; i++) {
??????? NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
??????? }
??? }];
???
??? //創(chuàng)建NSOperationQueue
??? NSOperationQueue * queue=[[NSOperationQueue alloc]init];
??? //把操作添加到隊(duì)列中
??? [queue addOperation:operation1];
??? [queue addOperation:operation2];
??? [queue addOperation:operation3];
}

-(void)test1
{
??? for (int i=0; i<5; i++) {
??? NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
??? }
}

-(void)test2
{
??? for (int i=0; i<5; i++) {
??? NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
??? }
}

@end

?

?

?

2015111392049597.png (889×273)

?

三、并發(fā)數(shù)
(1)并發(fā)數(shù):同時(shí)執(zhí)?行的任務(wù)數(shù).比如,同時(shí)開3個(gè)線程執(zhí)行3個(gè)任務(wù),并發(fā)數(shù)就是3
(2)最大并發(fā)數(shù):同一時(shí)間最多只能執(zhí)行的任務(wù)的個(gè)數(shù)。
(3)最?大并發(fā)數(shù)的相關(guān)?方法

?

復(fù)制代碼 代碼如下:

- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;

?

?


說(shuō)明:如果沒有設(shè)置最大并發(fā)數(shù),那么并發(fā)的個(gè)數(shù)是由系統(tǒng)內(nèi)存和CPU決定的,可能內(nèi)存多久開多一點(diǎn),內(nèi)存少就開少一點(diǎn)。
注意:num的值并不代表線程的個(gè)數(shù),僅僅代表線程的ID。
提示:最大并發(fā)數(shù)不要亂寫(5以內(nèi)),不要開太多,一般以2~3為宜,因?yàn)殡m然任務(wù)是在子線程進(jìn)行處理的,但是cpu處理這些過(guò)多的子線程可能會(huì)影響UI,讓UI變卡。

?

四、隊(duì)列的取消,暫停和恢復(fù)
?(1)取消隊(duì)列的所有操作

復(fù)制代碼 代碼如下:

?- (void)cancelAllOperations;


提?:也可以調(diào)用NSOperation的- (void)cancel?法取消單個(gè)操作

?

?(2)暫停和恢復(fù)隊(duì)列

復(fù)制代碼 代碼如下:

- (void)setSuspended:(BOOL)b; // YES代表暫停隊(duì)列,NO代表恢復(fù)隊(duì)列

?

- (BOOL)isSuspended; //當(dāng)前狀態(tài)


(3)暫停和恢復(fù)的適用場(chǎng)合:在tableview界面,開線程下載遠(yuǎn)程的網(wǎng)絡(luò)界面,對(duì)UI會(huì)有影響,使用戶體驗(yàn)變差。那么這種情況,就可以設(shè)置在用戶操作UI(如滾動(dòng)屏幕)的時(shí)候,暫停隊(duì)列(不是取消隊(duì)列),停止?jié)L動(dòng)的時(shí)候,恢復(fù)隊(duì)列。

?

五、操作優(yōu)先級(jí)
?(1)設(shè)置NSOperation在queue中的優(yōu)先級(jí),可以改變操作的執(zhí)?優(yōu)先級(jí)

復(fù)制代碼 代碼如下:

- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;


?(2)優(yōu)先級(jí)的取值

?

復(fù)制代碼 代碼如下:

?


NSOperationQueuePriorityVeryLow = -8L,

?

NSOperationQueuePriorityLow = -4L,

NSOperationQueuePriorityNormal = 0,

NSOperationQueuePriorityHigh = 4,

NSOperationQueuePriorityVeryHigh = 8


說(shuō)明:優(yōu)先級(jí)高的任務(wù),調(diào)用的幾率會(huì)更大。

?

六、操作依賴
(1)NSOperation之間可以設(shè)置依賴來(lái)保證執(zhí)行順序,?如一定要讓操作A執(zhí)行完后,才能執(zhí)行操作B,可以像下面這么寫

復(fù)制代碼 代碼如下:

[operationB addDependency:operationA]; // 操作B依賴于操作


(2)可以在不同queue的NSOperation之間創(chuàng)建依賴關(guān)系

?

?

?

2015111392125598.png (388×187)

注意:不能循環(huán)依賴(不能A依賴于B,B又依賴于A)。

(3)代碼示例

復(fù)制代碼 代碼如下:

#import "YYViewController.h"

?

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?


@implementation YYViewController

?

- (void)viewDidLoad
{
??? [super viewDidLoad];

??? //創(chuàng)建NSInvocationOperation對(duì)象,封裝操作
??? NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:nil];
??? NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil];
??? //創(chuàng)建對(duì)象,封裝操作
??? NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{
??????? for (int i=0; i<5; i++) {
??????????? NSLog(@"NSBlockOperation3--1----%@",[NSThread currentThread]);
??????? }
??? }];
??? [operation3 addExecutionBlock:^{
??????? for (int i=0; i<5; i++) {
??????? NSLog(@"NSBlockOperation3--2----%@",[NSThread currentThread]);
??????? }
??? }];
???
??? //設(shè)置操作依賴
??? //先執(zhí)行operation2,再執(zhí)行operation1,最后執(zhí)行operation3
??? [operation3 addDependency:operation1];
??? [operation1 addDependency:operation2];
???
??? //不能是相互依賴
//??? [operation3 addDependency:operation1];
//??? [operation1 addDependency:operation3];
???
??? //創(chuàng)建NSOperationQueue
??? NSOperationQueue * queue=[[NSOperationQueue alloc]init];
??? //把操作添加到隊(duì)列中
??? [queue addOperation:operation1];
??? [queue addOperation:operation2];
??? [queue addOperation:operation3];
}

?

復(fù)制代碼 代碼如下:

?


-(void)test1
{
??? for (int i=0; i<5; i++) {
??? NSLog(@"NSInvocationOperation--test1--%@",[NSThread currentThread]);
??? }
}

?

-(void)test2
{
??? for (int i=0; i<5; i++) {
??? NSLog(@"NSInvocationOperation--test2--%@",[NSThread currentThread]);
??? }
}

@end


打印查看:

?

?

?

2015111392149398.png (907×305)

A做完再做B,B做完才做C。
注意:一定要在添加之前,進(jìn)行設(shè)置。
提示:任務(wù)添加的順序并不能夠決定執(zhí)行順序,執(zhí)行的順序取決于依賴。使用Operation的目的就是為了讓開發(fā)人員不再關(guān)心線程。
?
?
5.操作的監(jiān)聽
可以監(jiān)聽一個(gè)操作的執(zhí)行完畢

復(fù)制代碼 代碼如下:

- (void (^)(void))completionBlock;
- (void)setCompletionBlock:(void (^)(void))block;


代碼示例

?

第一種方式:可以直接跟在任務(wù)后面編寫需要完成的操作,如這里在下載圖片后,緊跟著下載第二張圖片。但是這種寫法有的時(shí)候把兩個(gè)不相關(guān)的操作寫到了一個(gè)代碼塊中,代碼的可閱讀性不強(qiáng)。

復(fù)制代碼 代碼如下:

#import "YYViewController.h"

?

@interface YYViewController ()

@end

@implementation YYViewController

- (void)viewDidLoad
{
??? [super viewDidLoad];

??? //創(chuàng)建對(duì)象,封裝操作
??? NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
??????? NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);
??????? //.....下載圖片后繼續(xù)進(jìn)行的操作
??????? NSLog(@"--接著下載第二張圖片--");
??? }];
????
??? //創(chuàng)建隊(duì)列
??? NSOperationQueue *queue=[[NSOperationQueue alloc]init];
??? //把任務(wù)添加到隊(duì)列中(自動(dòng)執(zhí)行,自動(dòng)開線程)
??? [queue addOperation:operation];
}

@end


第二種方式:

?

復(fù)制代碼 代碼如下:

?


#import "YYViewController.h"

?

@interface YYViewController ()

@end

?

復(fù)制代碼 代碼如下:

?

?

@implementation YYViewController

- (void)viewDidLoad
{
??? [super viewDidLoad];

??? //創(chuàng)建對(duì)象,封裝操作
??? NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{
??????? for (int i=0; i<10; i++) {
??????????? NSLog(@"-operation-下載圖片-%@",[NSThread currentThread]);
??????? }
??? }];
???
??? //監(jiān)聽操作的執(zhí)行完畢
??? operation.completionBlock=^{
??????? //.....下載圖片后繼續(xù)進(jìn)行的操作
??????? NSLog(@"--接著下載第二張圖片--");
??? };
???
??? //創(chuàng)建隊(duì)列
??? NSOperationQueue *queue=[[NSOperationQueue alloc]init];
??? //把任務(wù)添加到隊(duì)列中(自動(dòng)執(zhí)行,自動(dòng)開線程)
??? [queue addOperation:operation];
}

@end


打印查看:

?

?

?

2015111392210341.png (880×155)

說(shuō)明:在上一個(gè)任務(wù)執(zhí)行完后,會(huì)執(zhí)行operation.completionBlock=^{}代碼段,且是在當(dāng)前線程執(zhí)行(2)。

上文是詳解iOS多線程應(yīng)用開發(fā)中使用NSOperation類的操作方法,相信大家都有了一定的了解,想要了解更多的技術(shù)信息,請(qǐng)繼續(xù)關(guān)注武林技術(shù)頻道吧!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 唐河县| 于田县| 左贡县| 凤台县| 古蔺县| 田阳县| 四平市| 佛山市| 南投县| 霍邱县| 昌平区| 华蓥市| 鄂伦春自治旗| 英吉沙县| 五家渠市| 丽江市| 鄢陵县| 乌拉特前旗| 辛集市| 金湖县| 彭水| 红河县| 天峨县| 岚皋县| 满城县| 时尚| 保靖县| 琼中| 昌图县| 谷城县| 嵊泗县| 合川市| 团风县| 富顺县| 文山县| 沂源县| 怀来县| 八宿县| 北票市| 大足县| 桃园市|