1. 程式人生 > >iOS多線程---NSOperation的常用操作

iOS多線程---NSOperation的常用操作

自動 最大並發數 com str 需要 循環 單個 監聽 sin

1.最大並發數:

- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    queue.maxConcurrentOperationCount = 3;

註:系統一般會根據內存大小自動設置並發數目,也可以自己設定,但是不要亂設,一般不要超過5個。 2.暫停和取消

(1)取消隊列的所有操作

- (void)cancelAllOperations;

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

(2)暫停和恢復隊列

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

- (BOOL)isSuspended; //當前狀態

3.操作優先級 : 設置NSOPeration的優先級,可以改變在NSOPerationQueue中的執行優先級

    [operation2 setQueuePriority:NSOperationQueuePriorityNormal];  //設置優先級
    NSOperationQueuePriority priority 
= operation2.queuePriority; //獲去操作的優先級

4.操作依賴

  有兩個操作,operationA:買飯和operationB:吃飯,必須要先買飯,然後再吃飯。操作B:吃飯 必須等到操作A:買飯 的操作完成後才能執行。這就是依賴關系。

  在NSOPeration中描述依賴用:[operationB addDependency:operationA];

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInvocationOperation *operationA = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction) object
:nil]; NSInvocationOperation *operationB = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testAction1) object:nil]; [operationB addDependency:operationA]; //定義隊列 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //將操作添加到隊列 [queue addOperation:operationA]; [queue addOperation:operationB]; } -(void)testAction { for (int i = 0; i < 5; i ++) { NSLog(@"給五個人買飯帶回宿舍----%@",[NSThread currentThread]); } } -(void)testAction1 { for (int i = 0; i < 5; i ++) { NSLog(@"五個人開始吃飯----%@",[NSThread currentThread]); } }

2017-06-18 11:33:26.765 demo[17639:3076261] 給五個人買飯帶回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.766 demo[17639:3076261] 給五個人買飯帶回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.767 demo[17639:3076261] 給五個人買飯帶回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.768 demo[17639:3076261] 給五個人買飯帶回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.769 demo[17639:3076261] 給五個人買飯帶回宿舍----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.770 demo[17639:3076261] 五個人開始吃飯----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.776 demo[17639:3076261] 五個人開始吃飯----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.777 demo[17639:3076261] 五個人開始吃飯----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.781 demo[17639:3076261] 五個人開始吃飯----<NSThread: 0x600000267b40>{number = 3, name = (null)}

2017-06-18 11:33:26.782 demo[17639:3076261] 五個人開始吃飯----<NSThread: 0x600000267b40>{number = 3, name = (null)}

註:用for循環更能看出操作的執行順序。

5.操作的監聽

有時候需要執行完一個操作再執行另個操作,可以將這兩個操作寫在一起,但是當代碼很多的時候,會造成閱讀性不強。

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        for (int i  = 0;  i < 5; i ++) {
            NSLog(@"給五個人買飯帶回宿舍----%@",[NSThread currentThread]);
        }
        
        for (int i  = 0;  i < 5; i ++) {
            NSLog(@"五個人開始吃飯----%@",[NSThread currentThread]);
        }
    }];
    
    //定義隊列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //將操作添加到隊列
    [queue addOperation:operation];

也可以分開寫:

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        for (int i  = 0;  i < 5; i ++) {
            NSLog(@"給五個人買飯帶回宿舍----%@",[NSThread currentThread]);
        }
    }];
    operation.completionBlock = ^{
        for (int i  = 0;  i < 5; i ++) {
            NSLog(@"五個人開始吃飯----%@",[NSThread currentThread]);
        }
    };
    
    //定義隊列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    //將操作添加到隊列
    [queue addOperation:operation];

2017-06-18 11:41:21.883 demo[17675:3084893] 給五個人買飯帶回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}

2017-06-18 11:41:21.885 demo[17675:3084893] 給五個人買飯帶回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}

2017-06-18 11:41:21.886 demo[17675:3084893] 給五個人買飯帶回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}

2017-06-18 11:41:21.888 demo[17675:3084893] 給五個人買飯帶回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}

2017-06-18 11:41:21.888 demo[17675:3084893] 給五個人買飯帶回宿舍----<NSThread: 0x600000071200>{number = 3, name = (null)}

2017-06-18 11:41:21.894 demo[17675:3084895] 五個人開始吃飯----<NSThread: 0x60800006c600>{number = 4, name = (null)}

2017-06-18 11:41:21.895 demo[17675:3084895] 五個人開始吃飯----<NSThread: 0x60800006c600>{number = 4, name = (null)}

2017-06-18 11:41:21.896 demo[17675:3084895] 五個人開始吃飯----<NSThread: 0x60800006c600>{number = 4, name = (null)}

2017-06-18 11:41:21.897 demo[17675:3084895] 五個人開始吃飯----<NSThread: 0x60800006c600>{number = 4, name = (null)}

2017-06-18 11:41:21.898 demo[17675:3084895] 五個人開始吃飯----<NSThread: 0x60800006c600>{number = 4, name = (null)}

註:可以看出當買飯的操作完成後,開啟一個新線程執行吃飯操作。

iOS多線程---NSOperation的常用操作