ios开发多线程一:了解-NSOperation的基本使用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self blockOperation];
}

/**
 *    1:NSOperation封装任务的两种方式:NSInvocationOperation,NSBlockOperation,其中两种方法若没指定队列,则默认是在主队列里去执行
 2:住家任务:addExecutionBlock:
 注意:如果一个操作中的任务数量大于1,那么会开子线程并发执行任务
 
 注意:不一定是子线程,有可能是主线程
 */



-(void)invocationOpeation
{
    
    //1.创建操作,封装任务
    /*
     第一个参数:目标对象 self
     第二个参数:调用方法的名称
     第三个参数:前面方法需要接受的参数 nil
     */
     NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
     NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
     NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download3) object:nil];
    
    //2.启动|执行操作
     [op1 start];
     [op2 start];
     [op3 start];
}

-(void)blockOperation
{
    //1.创建操作
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2----%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3----%@",[NSThread currentThread]);
    }];
    
    //追加任务
    //注意:如果一个操作中的任务数量大于1,那么会开子线程并发执行任务
    //注意:不一定是子线程,有可能是主线程
    [op3 addExecutionBlock:^{
        NSLog(@"4---%@",[NSThread currentThread]);
    }];
    
    [op3 addExecutionBlock:^{
        NSLog(@"5---%@",[NSThread currentThread]);
    }];
    
    [op3 addExecutionBlock:^{
        NSLog(@"6---%@",[NSThread currentThread]);
    }];
    
    //2.启动
    [op1 start];
    [op2 start];
    [op3 start];
}

-(void)download1
{
    NSLog(@"%s----%@",__func__,[NSThread currentThread]);
}

-(void)download2
{
    NSLog(@"%s----%@",__func__,[NSThread currentThread]);
}


-(void)download3
{
    NSLog(@"%s----%@",__func__,[NSThread currentThread]);
}

@end

###2.NSOperation

- 2.1 NSOperation基本使用

(1)相关概念

    01 NSOperation是对GCD的包装

    02 两个核心概念【队列+操作】

(2)基本使用

    01 NSOperation本身是抽象类,只能只有它的子类

    02 三个子类分别是:NSBlockOperation、NSInvocationOperation以及自定义继承自NSOperation的类

    03 NSOperation和NSOperationQueue结合使用实现多线程并发

(3)相关代码

```objc

//  01 NSInvocationOperation

    //1.封装操作

    /*

     第一个参数:目标对象

     第二个参数:该操作要调用的方法,最多接受一个参数

     第三个参数:调用方法传递的参数,如果方法不接受参数,那么该值传nil

     */

    NSInvocationOperation *operation = [[NSInvocationOperation alloc]

                                        initWithTarget:self selector:@selector(run) object:nil];

    //2.启动操作

    [operation start];

-------------------------------------------------

    //  02 NSBlockOperation

    //1.封装操作

    /*

     NSBlockOperation提供了一个类方法,在该类方法中封装操作

     */

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

        //在主线程中执行

        NSLog(@"---download1--%@",[NSThread currentThread]);

    }];

    //2.追加操作,追加的操作在子线程中执行

    [operation addExecutionBlock:^{

        NSLog(@"---download2--%@",[NSThread currentThread]);

    }];

    [operation addExecutionBlock:^{

         NSLog(@"---download3--%@",[NSThread currentThread]);

    }];

    //3.启动执行操作

    [operation start];

----------------------------------------------

// 03 自定义NSOperation

    //如何封装操作?

    //自定义的NSOperation,通过重写内部的main方法实现封装操作

    -(void)main

    {

        NSLog(@"--main--%@",[NSThread currentThread]);

    }

    //如何使用?

    //1.实例化一个自定义操作对象

    XMGOperation *op = [[XMGOperation alloc]init];

    //2.执行操作

    [op start];

```

原文地址:https://www.cnblogs.com/cqb-learner/p/5851503.html