串聯NSOperation知識點

2021-09-11 14:15:59 字數 2328 閱讀 4274

建立nsoperationqueue

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

複製**

建立nsoperation子類物件

nsoperation *operation = [[nsoperation alloc] init];

需要重寫nsoperation任務執行函式。

myoperation *operation = [[myoperation alloc] init];

複製**

operation.completionblock = ^() {};

// 需注意,

//completionbolck的執行不在主線程,也不在operation的佇列中。

//是系統分配的乙個後台執行緒

複製**

將nsoperation加入nsoperationqueue

[queue addoperation:operation];

複製**

ios為了讓我們更好的使用nsoperation,自定義了很多繼承類

nsblockoperation

+ (instancetype)blockoperationwithblock:(void(^)(void))block;

複製**

nsinvocationoperation

- (nullable instancetype)initwithtarget:(id)target selector:(sel)sel object:(nullable id)arg;

複製**

系統預設提供:

@property nsinteger maxconcurrentoperationcount;

// = 1時,為serial queue

// > 1時,為concurrent queue

@property (readonly)nsuinteger operationcount;

//表示當前正在執行的queue中有多少任務。

複製**

nsoperation支援取消。封裝,能適應更複雜的操作和提供更精細化的控制。

既是queue被cancel了,最終仍會被執行。

nsoperation不需要group的概念。

- (void)waituntilfinished;

- (void)waituntilalloperationsarefinished;

複製**

@inte***ce nsoperation : nsobject 

@property (readonly, copy)nsarray*dependencies;

複製**

迴圈依賴顯示迴圈依賴 隱式迴圈依賴

優先順序

@property nsoperationqueuepriority queuepriority;

複製**

operation進入queue的原則是先進先出。但當優先順序大的operation準備進入queue時,它會插隊到比他小的operation前面。但不會替換掉正在執行的operation。

@property (getter = issuspended)bool suspended;

@property (nullable, copy)nsstring *name;

@property nsqualityofservice qualityofservice;

複製**

nsoperation和nsoperationqueue的基本概念

自定義nsoperation子類

nsoperation狀態和取消

nsoperation dependency

waituntilfinished

name,優先順序,暫停

NSOperation 簡單使用

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

NSOperation 操作 簡介

nsoperation 使用1.簡介 重點 理解操作 nsoperation 和操作佇列 nsoperationqueue nsoperation 使用2.nsoperation 重點 1.nsblockoperation,nsinvocationoperation 的簡單使用.新增更多操作 op2...

NSOperation基本操作

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