iOS多线程编程之NSOperation

资料来源 NShipster之NSOperation

对于异步执行技术GCD的API已经非常简单高效,但对于某些task之间存在依赖或是监测task的执行状态等需求GCD就显得不那么高效;因此cocoa框架中就提供了一个使用面向对象思想来解决异步操作的NSOperation,同时结合NSOperationQueue会使得异步操作更加简单高效;

NSOperation

NSOperation是一个抽象基类,只能实例化子类才能使用;开发框架中已经存在了两个实例NSBlockOperation和NSInvocationOperation

NSBlockOperation

NSBlockOperation实例化操作可以在block中执行task,并且当task只有一个的时候task在主线程中执行,当task增加时会另外创建线程执行task;

NSBlockOperation 在调用start方法时所有添加的task会hold住主线程,也就是所有的task执行完毕后才继续执行主线程的其他操作

NSBlockOperation 中添加多个task时,各个task的执行顺序不一定,但是第添加的task一定会在主线程中被执行

NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"blockOp1:%@",[NSThread currentThread]);
}];
[blockOperation addExecutionBlock:^{
    NSLog(@"blockOp2:%@",[NSThread currentThread]);
}];
[blockOperation addExecutionBlock:^{
    NSLog(@"blockOp3:%@",[NSThread currentThread]);
}];
[blockOperation addExecutionBlock:^{
    [NSThread sleepForTimeInterval:3.0f];
    NSLog(@"blockOp4:%@",[NSThread currentThread]);
}];
blockOperation.completionBlock = ^{
    NSLog(@"blockOperation over!");
};
[blockOperation start];
NSLog(@"run over!!!:%@",[NSThread currentThread]);
/*
  HZFoundation[46406:1977543] blockOp3:{number = 2, name = (null)}
  HZFoundation[46406:1977541] blockOp2:{number = 3, name = (null)}
  HZFoundation[46406:1977517] blockOp1:{number = 1, name = main}
  HZFoundation[46406:1977542] blockOp4:{number = 4, name = (null)}
  HZFoundation[46406:1977517] run over!!!:{number = 1, name = main}
 */

根据上述log的输出可用GCD模拟一下NSBlockOperation内部实现

  -(void)implementationBlockOperation{
// 添加task
OperationBlock block1=^{
    NSLog(@"block1%@",[NSThread currentThread]);
};
OperationBlock block2=^{
    NSLog(@"block2%@",[NSThread currentThread]);
};
OperationBlock block3=^{
    NSLog(@"block3%@",[NSThread currentThread]);
};
OperationBlock block4=^{
    NSLog(@"block4%@",[NSThread currentThread]);
};
OperationBlock block5=^{
    [NSThread sleepForTimeInterval:3.0f];
    NSLog(@"block5%@",[NSThread currentThread]);
};
// 所有执行task的集合
NSArray* operationBlcoks = [NSArray arrayWithObjects:block1,block2,block3,block4,block5,nil];
//根据上面打印的block执行线程名称可以看到,线程名称各不相同,由此推测block的执行在一个并发队列中
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.*.implementationBlockOperation", DISPATCH_QUEUE_CONCURRENT);
//打印的内容可以看到NSOperation是block住主线程的,也就是所有的任务执行完毕后才继续执行task,能够执行此操作的dispatch_group最合适
dispatch_group_t dispatchGroup = dispatch_group_create();
for (NSInteger index=0; index

上述实现代码唯一的不足时第一task执行顺序会影响其他task,因为它总是第一个被执行,目前还没有考虑出合适的实现方法;

NSInvocationOperation

NSInvocationOperation的使用和UIControl的使用模式很相似

-(void)operationInvocation{
   NSInvocationOperation* operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operationAction) object:nil];
[operation1 start];
}

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

NSOperationQueue

  // 创建operation
NSBlockOperation* blockOperation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"blockOp1:%@",[NSThread currentThread]);
}];
NSBlockOperation* blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"blockOp2:%@",[NSThread currentThread]);
}];
// 创建operationQueue
NSOperationQueue* operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:blockOperation];
[operationQueue addOperation:blockOperation1];  
[operationQueue setMaxConcurrentOperationCount:4]; // 设置task最大执行并发数
[blockOperation addDependency:blockOperation1];// operation之间添加依赖关系
[operationQueue waitUntilAllOperationsAreFinished];// 等待所有的operation执行完毕后
[operationQueue cancelAllOperations];//取消所有operation操作

你可能感兴趣的:(iOS多线程编程之NSOperation)