【iOS出租屋进阶】之多线程 NSOperation 详解

NSOperation 简介

1). NSOperation是基于GCD之上的更高一层封装, 拥有更多的API
2). 在NSOperationQueue中, 可以指定各个NSOperation之间的依赖关系.
3). 用KVO可以方便的监测NSOperation的状态
4). 更高的可定制能力, 你可以继承NSOperation实现可复用的逻辑模块.

简单使用

**NSInvocationOperation **
NSInvocationOperation * invoOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testNSInvocationOperation) object:nil]; 
- (void)testNSInvocationOperation {
    NSlog(@"1");
}
//[invoOperation start];

**NSBlockOperation**
NSBlockOperation * blockOperation = [[NSBlockOperation 
blockOperationWithBlock:^{
   NSLog(@"1");
}];
[blockOperation addExecutionBlock:^{
  NSLog(@"2");
}];
//[blockOperation start];

**NSOPerationQueue**
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[queue addOperation:invoOperation];
[queue addOperation:blockOperation];

**添加依赖关系实现同步执行**
NSInvocationOperation * invoOperation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(testNSInvocationOperation2) object:nil];
- (void)testNSInvocationOperation {
    NSlog(@"2");
}
[invoOperation2 addDependency:invoOperation2];
[queue addOperation:invoOperation2];

详细请点击

你可能感兴趣的:(【iOS出租屋进阶】之多线程 NSOperation 详解)