NSOperation 操作 簡介

2021-07-09 09:30:28 字數 2082 閱讀 1810

nsoperation

使用1.簡介

重點:理解操作

nsoperation

和操作佇列

nsoperationqueue!

/*----------------------------  nsoperation

使用2.nsoperation ------------------------------*/重點:

1.nsblockoperation, nsinvocationoperation

的簡單使用.];

//新增更多操作

[op2 addexecutionblock:^];

[op2 addexecutionblock:^];

//只要

nsblockoperation

中封裝的運算元

> 1,

呼叫start

方法之後就會開啟多條執行緒併發執行

//如果

nsblockoperation

中封裝的運算元

== 1,

呼叫start

方法之後

,不會開啟新執行緒

,只會在當前執行緒執行操作

[op2 start];注意:

只要nsblockoperation

中封裝的運算元

>1,

就會非同步執行這些操作

.(將操作新增到

nsoperationqueue

中或者直接呼叫

start

方法都會開啟多條執行緒非同步執行).}

/*---------------------------- nsoperation

使用3.nsoperationqueue --------------------------*/重點:

將操作新增到佇列中;];

// 1.

建立乙個

nsoperationqueue

nsoperationqueue *queue = [[nsoperationqueue alloc] init];

// 2.

將操作新增到佇列中

.[queue addoperation:op1];

[queue addoperation:op2];注意:

另外一種新增操作到佇列中的方法

: block

[queue addoperationwithblock:^];

推薦使用

: block

//簡單

.自己哪個使用熟練就用哪個.注意

:佇列中任務的執行是無序的.}

/*-------------------------------- nsoperation

使用4.

常見用法

1 -------------------------------*/重點:

1.設定操作依賴.2.

設定最大併發數

./*-------------------------------- nsoperation

使用5.

常見用法

2 -------------------------------*/重點:

1.佇列的取消/暫停

/恢復2.執行緒間通訊

.注意問題

:為毛要取消

/恢復佇列

?在什麼時候用?//

開始滾動的時候暫停佇列中的任務

.- (

void

)scrollviewwillbegindragging:(uiscrollview *)scrollview

//滾動結束的時候恢復佇列中的任務

.- (

void

)scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(

bool

)decelerate

2.執行緒間通訊//,

主線程設定

.nsoperationqueue *queue = [[nsoperationqueue alloc] init];

[queue addoperationwithblock:^];

}];}

NSOperation基本操作

nsoperation基本操作 一 併發數 1 併發數 同時執?行的任務數.比如,同時開3個執行緒執行3個任務,併發數就是3 2 最大併發數 同一時間最多只能執行的任務的個數。3 最?大併發數的相關?方法 nsinteger maxconcurrentoperationcount void setm...

11 NSOperation的其他操作

1 最大併發數 1 什麼是併發數 同時執行的任務書 比如 同時開3個執行緒執行3個任務,併發數就是3 2 最大併發數的相關方法 nsinteger maxconcurrentoperationcount void setmaxconcurretoperationcount nsinteger cnt...

NSOperation 簡單使用

nsoperation 是一種多執行緒技術 起基本原理是把相應地操作放在佇列裡邊 然後有佇列分發處理。佇列的建立 初始化乙個佇列 return 初始化好的對列 nsoperationqueue myqueue return myqueue 向佇列中新增操作 pragma mark 同時併發執行緒數量...