多线程之NSOperation

2017-11-25  本文已影响12人  WenJim

简单介绍

NSOperation的子类

NSInvocationOperation

- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg;
- (void)start;

一旦执行操作,就会调用target的sel方法

-(void)invocationOperationWithQueue
{
    // 1. 创建操作,封装任务
    /*
     第一参数: 目标对象 self
     第二参数: 调用方法的名称
     第三参数: 前面方法需要接受的参数 nil
     */
    NSInvocationOperation * op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
    
    NSInvocationOperation * op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
    
    NSInvocationOperation * op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
    
    // 2. 创建队列
    /*
     GCD:
     串行类型: create & 主队列
     并发类型: create & 全局并发队列
     
     NSOperation:
     主队列:   [NSOperationQueue mainQueue]; 和 GCD 中的主队列一样
     非主队列: [[NSOperationQueue alloc] init]; 非常特殊(同时具备并罚和串行的功能)
     // 默认情况下,非主队列示并发队列
     */
    NSOperationQueue * queue = [[NSOperationQueue alloc] init];
    
    // 3. 添加操作到队列中
    [queue addOperation:op1];  // 内部已经调用了 [op1 start];
    [queue addOperation:op2];
    [queue addOperation:op3];
}

NSBlockOperation

+ (id)blockOperationWithBlock:(void (^)(void))block;
- (void)addExecutionBlock:(void (^)(void))block;
-(void)blockOPerationWithQueue
{
    NSBlockOperation * blockOP = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1---------%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation * blockOP2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"2---------%@",[NSThread currentThread]);
    }];
    
    NSBlockOperation * blockOP3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3---------%@",[NSThread currentThread]);
        [self download];
    }];
    
    // 追加任务
    [blockOP2 addExecutionBlock:^{
        NSLog(@"4---------%@",[NSThread currentThread]);
    }];
    
    [blockOP2 addExecutionBlock:^{
        NSLog(@"5---------%@",[NSThread currentThread]);
    }];
    
    [blockOP2 addExecutionBlock:^{
        NSLog(@"6---------%@",[NSThread currentThread]);
    }];
    
    
    // 2. 创建队列
    NSOperationQueue * queue = [[NSOperationQueue alloc] init];
    
    // 3. 添加操作到队列中
    [queue addOperation:blockOP];
    [queue addOperation:blockOP2];
    [queue addOperation:blockOP3];
    
    
    // 简便方法
    // 1) 创建操作,
    // 2) 添加操作到队列中
    [queue addOperationWithBlock:^{
        NSLog(@"7---------%@",[NSThread currentThread]);
    }];
}

NSOperationQueue

- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;
- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cat;

队列的取消、暂停、恢复

- (void)cancelAllOperations;

提示:也可以调用NSOperation的- (void)cancel方法取消单个操作

- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列
- (BOOL)isSuspended;

操作依赖

[operationB addDependency:operationA]; // 操作B依赖于操作A
操作依赖关系.png

操作的监听

可以监听一个操作的执行完毕
- (void (^)(void))completionBlock;
- (void)setCompletionBlock:(void (^)(void))block;

自定义NSOperation

上一篇下一篇

猜你喜欢

热点阅读