iOS开发[0002]iOS线程相关iOS多线程开发指南

iOS多线程实现方案之 -- NSOperation

2016-08-02  本文已影响400人  devZhang

前面已经总结了 iOS 多线程中的 NSThread 和 GCD 方法的相关知识点(文末有之前文章链接), 这篇文章,就是总结一下 iOS 多线程实现方案中的最后一个 NSOperation

NSOperation 作用

NSOperation 和 NSOperationQueue 实现多线程的步骤

  1. 先将需要执行的操作封装到一个 NSOperation 对象中
  2. 然后将 NSOperation 对象添加到 NSOperationQueue 中
  3. 系统会自动将 NSOperationQueue 中的 NSOperation 取出来
  4. 讲取出来的 NSOperation 封装的操作放到一条新线程中执行

NSOperation 的子类

NSOperation 是个抽象类, 并不具备封装操作的能力, 必须要使用它的子类

使用 NSOperation 子类的方法有三种

  1. NSInvocationOperation
  2. NSBlockOperation
  3. 自定义子类继承 NSOperation, 实现内部相应的方法

NSInvocationOperation

NSInvocationOperation 在实际中很少使用, 因为其作用不大,从一段代码看下:

    // 创建操作并封装任务
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task) object:nil];
    
    // 启动操作
    [op1 start];

方法实现:

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

打印结果:

2016-07-28 17:56:39.700 NSOperation[42813:1677544] -[ViewController task]--<NSThread: 0x7fa561605b70>{number = 1, name = main}

也能执行任务, 是在主线程中执行的, 但是这样有点多此一举了, 在上面直接使用[self task];同样能实现效果,也是在主线程中执行.
所以, 实际开发中,NSInvocationOperation几乎不用.

NSBlockOperation

先来看一下用NSBlockOperation类方法创建对象,并使用,会是什么效果

    // 创建操作
    NSBlockOperation *bop1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1--%@", [NSThread currentThread]);
    }];
    
    NSBlockOperation *bop2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2--%@", [NSThread currentThread]);
    }];
    
    NSBlockOperation *bop3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3--%@", [NSThread currentThread]);
    }];
    
    // 启动
    [bop1 start];
    [bop2 start];
    [bop3 start];

打印结果:

2016-07-28 18:06:40.599 NSOperation[43352:1684737] 1--<NSThread: 0x7f8899701230>{number = 1, name = main}
2016-07-28 18:06:40.600 NSOperation[43352:1684737] 2--<NSThread: 0x7f8899701230>{number = 1, name = main}
2016-07-28 18:06:40.600 NSOperation[43352:1684737] 3--<NSThread: 0x7f8899701230>{number = 1, name = main}

可见此时依然没有开启子线程区执行操作,那么除了这个类方法,有没有其他方法呢? 先去源文件里看看, 发现还有一个- (void)addExecutionBlock:(void (^)(void))block;的对象方法, 它就是用来追加任务的


那么就来用一下这个方法, 给任务3追加任务
// 追加任务
    [bop3 addExecutionBlock:^{
        NSLog(@"4--%@", [NSThread currentThread]);
    }];
        
    [bop3 addExecutionBlock:^{
        NSLog(@"5--%@", [NSThread currentThread]);
    }];
    
    [bop3 addExecutionBlock:^{
        NSLog(@"6--%@", [NSThread currentThread]);
    }];

执行效果:


总结: 如果一个操作中的任务量大于1, 就会开启子线程并发执行任务,但是不一定全是子线程

NSInvocationOperation 配合 NSOperationQueue 使用

    // 创建操作并封装任务
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil];
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task2) object:nil];
    NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task3) object:nil];
    
    // 创建队列
    /*
     GCD:
     串行类型: create & 主队列
     并发类型: create & 全局并发队列
     NSOperation:
     主队列: [NSOperationQueue mainQueue] 与 GCD 中的主队列一样(串行队列)
     非主队列: [[NSOperationQueue alloc] init] 非常特殊(同时具备并发和串行的功能)
     // 默认情况,非主队列是并发队列
     */
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 添加操作到队列,  内部已经调用了 start 方法
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];

下面的三个方法都打印一下任务标号和当前线程, 代码简单就不传上来了, 直接看打印结果:

2016-07-28 18:36:16.280 NSOperationQueue 基本使用[45348:1714080] 1--<NSThread: 0x7f93415e3c40>{number = 2, name = (null)}
2016-07-28 18:36:16.280 NSOperationQueue 基本使用[45348:1714084] 2--<NSThread: 0x7f9341712970>{number = 3, name = (null)}
2016-07-28 18:36:16.280 NSOperationQueue 基本使用[45348:1714079] 3--<NSThread: 0x7f93416093d0>{number = 4, name = (null)}

此时就开启了子线程去执行任务.

NSBlockOperation 配合 NSOperationQueue 使用

也是直接上代码:

    // 创建操作
    NSBlockOperation *bop1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1--%@", [NSThread currentThread]);
    }];
    
    NSBlockOperation *bop2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2--%@", [NSThread currentThread]);
    }];
    
    NSBlockOperation *bop3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3--%@", [NSThread currentThread]);
    }];
    
    // 创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    [queue addOperation:bop1];
    [queue addOperation:bop2];
    [queue addOperation:bop3];

通过打印结果看出此时也开启了子线程区执行任务.
同样,这个时候还可以在给某一操作追加任务, 比如给任务2追加3个任务

    // 追加任务
    [bop2 addExecutionBlock:^{
        NSLog(@"4--%@", [NSThread currentThread]);
    }];
    
    [bop2 addExecutionBlock:^{
        NSLog(@"5--%@", [NSThread currentThread]);
    }];
    
    [bop2 addExecutionBlock:^{
        NSLog(@"6--%@", [NSThread currentThread]);
    }];

此时打印结果可知, 追加的任务全部是在子线程中并发执行的.

自定义 NSOperation

创建一个继承字 NSOperation 的自定义类


在要使用的地方直接导入头文件, 然后创建对象操作
    // 封装操作
    XFOperation *op1 = [[XFOperation alloc] init];
    XFOperation *op2= [[XFOperation alloc] init];
    
    // 创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    // 添加到 queue
    [queue addOperation:op1];
    [queue addOperation:op2];

此时还没有任务, 需要在自定义类的.m 文件里重写main方法,告知要执行什么任务.

#import "XFOperation.h"

@implementation XFOperation

- (void)main {
    NSLog(@"XFOPeration - %@", [NSThread currentThread]);
}

@end

执行, 打印结果如下:

2016-07-28 22:15:10.018 NSOperationQueue 基本使用[58640:1854583] XFOPeration - <NSThread: 0x7f9200e0d370>{number = 3, name = (null)}
2016-07-28 22:15:10.018 NSOperationQueue 基本使用[58640:1854584] XFOPeration - <NSThread: 0x7f9200db14b0>{number = 2, name = (null)}

两个操作都是在子线程执行的

相关文章
iOS 多线程知识点总结之: 进程和线程
iOS 多线程实现方案之 -- NSThread
iOS多线程实现方案之 -- GCD
iOS - GCD 编程

上一篇下一篇

猜你喜欢

热点阅读