美文网首页多线程开发
NSOperation && NSOperationQueue

NSOperation && NSOperationQueue

作者: Andy_Livings | 来源:发表于2020-04-30 19:08 被阅读0次

    NSOperation

    - (void)download {
        
        NSLog(@"download-----%@", [NSThread currentThread]);
    }
    
    1、NSInvocationOperation - 同步执行
    
    /// 1、NSInvocationOperation - 同步执行
    - (IBAction)createNSInvocationSynchronousExecution {
        
         // 创建操作
        NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        
        // operation直接调用start,是同步执行(在当前线程执行操作)
        [operation1 start];
        [operation2 start];
        [operation3 start];
        
    }
    
    2、NSInvocationOperation - 异步执行
    /// 2、NSInvocationOperation - 异步执行
    - (IBAction)createNSInvocationOperationAsynchronousExecution {
        
        // 创建队列
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        
        // 创建操作
        NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        
        NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        
        NSInvocationOperation *operation3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        
        // 添加操作到队列中,会自动异步执行
        [queue addOperation:operation1];
        [queue addOperation:operation2];
        [queue addOperation:operation3];
    }
    
    3、NSBlockOperation - 同步执行1
    - (IBAction)createNSBlockOperationAsynchronousExecution1 {
        
        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
    
            NSLog(@"---下载图片----1---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
    
            NSLog(@"---下载图片----2---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
    
            NSLog(@"---下载图片----3---%@", [NSThread currentThread]);
        }];
        
        [operation1 start];
        [operation2 start];
        [operation3 start];
        // 同步执行
    }
    
    4、NSBlockOperation - 同步执行2
    /// 4、NSBlockOperation - 同步执行2
    - (IBAction)createNSBlockOperationAsynchronousExecution2 {
        
        NSBlockOperation *operation = [[NSBlockOperation alloc] init];
        
        [operation addExecutionBlock:^{
            
            NSLog(@"---下载图片----1---%@", [NSThread currentThread]);
        }];
        
        [operation addExecutionBlock:^{
            
            NSLog(@"---下载图片----2---%@", [NSThread currentThread]);
        }];
        
        [operation addExecutionBlock:^{
            
            NSLog(@"---下载图片----3---%@", [NSThread currentThread]);
        }];
        
        [operation start];
        // 同步执行
    }
    
    5、创建队列 - 异步执行
    /// 5、创建队列 - 异步执行
    - (IBAction)createNSBlockOperationAsynchronousExecution3 {
        
        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----11---%@", [NSThread currentThread]);
        }];
        
        [operation1 addExecutionBlock:^{
            
            NSLog(@"---下载图片----12---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----2---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----3---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----4---%@", [NSThread currentThread]);
        }];
        
        // 1.创建队列
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        
        // 2.添加操作到队列中(自动异步执行)
        [queue addOperation:operation1];
        [queue addOperation:operation2];
        [queue addOperation:operation3];
        [queue addOperation:operation4];
    }
    
    6、主队列 - 在主线程中同步执行
    /// 6、主队列 - 在主线程中同步执行
    - (IBAction)createNSBlockOperationAsynchronousExecution4 {
        
        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----11---%@", [NSThread currentThread]);
        }];
        
        [operation1 addExecutionBlock:^{
            
            NSLog(@"---下载图片----12---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----2---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----3---%@", [NSThread currentThread]);
        }];
        
        NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
            
            NSLog(@"---下载图片----4---%@", [NSThread currentThread]);
        }];
        
        // 主队列
        NSOperationQueue *queue = [NSOperationQueue mainQueue];
        
        // 2.添加操作到队列中(自动同步执行)
        [queue addOperation:operation1];
        [queue addOperation:operation2];
        [queue addOperation:operation3];
        [queue addOperation:operation4];
    }
    

    如下图:


    Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-30 at 17.49.28.png

    NSOperationQueue

    1、设置依赖
    /// 设置依赖
    - (IBAction)dependencyOperation {
        
        /**
         假设有A、B、C、D四个操作,要求:
         1. 4个操作都异步执行
         2.操作D依赖于操作C
         3. 操作C依赖于操作B
         4. 操作B依赖于操作A
         */
        
        // 1.创建一个队列(非主队列)
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        
        // 2.创建4个操作
        NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"A-----%@", [NSThread currentThread]);
        }];
        
    //    [operationA addExecutionBlock:^{
    //
    //        NSLog(@"A2---%@", [NSThread currentThread]);
    //    }];
    //
    //    [operationA setCompletionBlock:^{
    //
    //        NSLog(@"AAAAA---%@", [NSThread currentThread]);
    //    }];
        
        NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"B-----%@", [NSThread currentThread]);
        }];
        NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"C-----%@", [NSThread currentThread]);
        }];
        NSBlockOperation *operationD = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"D-----%@", [NSThread currentThread]);
        }];
        
        // 设置依赖
        [operationB addDependency:operationA];
        [operationC addDependency:operationB];
        [operationD addDependency:operationC];
        
        // 3.添加操作到队列中(自动异步执行任务)
        [queue addOperation:operationD];
        [queue addOperation:operationC];
        [queue addOperation:operationA];
        [queue addOperation:operationB];
    }
    
    2、设置最大并发
    /// 设置最大并发
    - (IBAction)setMaxOperationCount {
        
        // 添加操作到队列中(自动异步执行任务,并发)
        NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"下载图片1---%@", [NSThread currentThread]);
        }];
        NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"下载图片2---%@", [NSThread currentThread]);
        }];
        NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"下载图片3---%@", [NSThread currentThread]);
        }];
        NSBlockOperation *operation4 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"下载图片4---%@", [NSThread currentThread]);
        }];
        NSInvocationOperation *operation5 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
        
        [self.queue addOperation:operation1];
        [self.queue addOperation:operation2];
        [self.queue addOperation:operation3];
        [self.queue addOperation:operation4];
        [self.queue addOperation:operation5];
        
        [self.queue addOperationWithBlock:^{
            NSLog(@"下载图片5---%@", [NSThread currentThread]);
        }];
        [self.queue addOperationWithBlock:^{
            NSLog(@"下载图片6---%@", [NSThread currentThread]);
        }];
        [self.queue addOperationWithBlock:^{
            NSLog(@"下载图片7---%@", [NSThread currentThread]);
        }];
        [self.queue addOperationWithBlock:^{
            NSLog(@"下载图片8---%@", [NSThread currentThread]);
        }];
        [self.queue addOperationWithBlock:^{
            NSLog(@"下载图片9---%@", [NSThread currentThread]);
        }];
        
    //    [queue cancelAllOperations];
    }
    
    
    
    - (void)download
    {
        NSLog(@"download---%@", [NSThread currentThread]);
    }
    
    - (void)didReceiveMemoryWarning {
        
        [super didReceiveMemoryWarning];
        
        [self.queue cancelAllOperations]; // 取消队列中的所有任务(不可恢复)
    }
    
    #pragma mark- scrollViewDelegate
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        
        [self.queue setSuspended:YES]; // 暂停队列中的所有任务
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        
        [self.queue setSuspended:NO]; // 恢复队列中的所有任务
    }
    
    #pragma mark- lazying
    
    - (NSOperationQueue *)queue {
        if (!_queue) {
            
            // 1.创建一个队列(非主队列)
            _queue = [[NSOperationQueue alloc] init];
            
            // 2.设置最大并发(最多同时并发执行3个任务)
            _queue.maxConcurrentOperationCount = 3;
        }
        return _queue;
    }
    

    如下图:


    Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-30 at 18.51.23.png

    具体实现步骤,请看demo内容。

    demo下载地址

    相关文章

      网友评论

        本文标题:NSOperation && NSOperationQueue

        本文链接:https://www.haomeiwen.com/subject/wdptghtx.html