ios多线程 -- NSOperation 简介

Posted zper

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios多线程 -- NSOperation 简介相关的知识,希望对你有一定的参考价值。

NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
1)先将需要执行的操作封装到一个NSOperation对象中
2)然后将NSOperation对象添加到NSOperationQueue中
3)系统会⾃动将NSOperationQueue中的NSOperation取出来
4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏

NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
1)NSInvocationOperation
2)NSBlockOperation
3)自定义子类继承NSOperation,实现内部相应的⽅法

NSInvocationOperation 开启任务方法

//用 NSInvocationOperation 创建线程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];
//在当前线程中同步执行
[invocation start];

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

同步

将创建的任务添加到队列中,异步执行

- (void)invocationOperation{
    //用 NSInvocationOperation 创建线程
    NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];  
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //从对列中取出任务执行(异步)
    [queue addOperation:invocation];

}

- (void)download:(NSString *)url{
    NSLog(@"%@ --- %@", [NSThread currentThread], url);
}

异步
注意:操作对象默认在主线程中执行,只有添加到队列中才会开启新的线程。即默认情况下,如果操作没有放到队列中queue中,都是同步执行。只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作

NSBlockOperation 开启任务的方法

NSBlockOperation 添加任务
- (void)blockOperation2{
    //创建任务
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [operation1 start];
}

这里写图片描述

注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作

- (void)blockOperation2{
    //注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作
    //创建任务
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片2 --- %@", [NSThread currentThread]);
    }];

    [operation1 addExecutionBlock:^{
        NSLog(@"下载图片3 --- %@", [NSThread currentThread]);
    }];

    [operation1 start];
}

这里写图片描述

NSOperationQueue

NSOperationQueue的作⽤:NSOperation可以调⽤start⽅法来执⾏任务,但默认是同步执行的
如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中,自动执行操作,自动开启线程
获取 NSOperationQueue 队列:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//并发队列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列

添加任务有两个方法:

- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block; 

实例代码

- (void)blockOperation{
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];//并发队列
    //NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列

    //创建任务
    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]);
    }];

    NSBlockOperation *operation5 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片5 --- %@", [NSThread currentThread]);
    }];

    //将任务添加到队列中
    [queue addOperation:operation1];
    [queue addOperation:operation2];
    [queue addOperation:operation3];
    [queue addOperation:operation4];
    [queue addOperation:operation5];
}

这里写图片描述

- (void)queueAddBlock{
    //创建全局队列(实现异步并发执行)
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    //直接向队列中添加任务
    [queue addOperationWithBlock:^{
         NSLog(@"下载图片1 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片2 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片3 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片4 --- %@", [NSThread currentThread]);
    }];

    [queue addOperationWithBlock:^{
        NSLog(@"下载图片5 --- %@", [NSThread currentThread]);
    }];
}

这里写图片描述

以上是关于ios多线程 -- NSOperation 简介的主要内容,如果未能解决你的问题,请参考以下文章

iOS多线程编程--NSOperation(转)

iOS 开发多线程 —— NSOperation

iOS开发多线程--(NSOperation/Queue)

iOS开发多线程编程2 - NSOperation

iOS多线程---NSOperation介绍和使用

iOS-多线程之NSOperation