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

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)}

注:可以看出当买饭的操作完成后,开启一个新线程执行吃饭操作。 

 
 
原文地址:https://www.cnblogs.com/huadeng/p/7043893.html