首页 > 代码库 > 任务之间的依赖(NSOperation)
任务之间的依赖(NSOperation)
//任务依赖:如果A依赖B,等B执行完成后,再去执行A
-(void)dependency
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSBlockOperation *bp1 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10; i++) {
NSLog(@"--1-%@-",[NSThread currentThread]);
}
}];
NSBlockOperation *bp2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10; i++) {
NSLog(@"--2-%@-",[NSThread currentThread]);
}
}];
NSBlockOperation *bp3 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 10; i++) {
NSLog(@"--3-%@-",[NSThread currentThread]);
}
}];
//添加12间的依赖,前者依赖后者,先执行后者,在执行前者
//注意:任务之间不能相互依赖
[bp1 addDependency:bp2];
[queue addOperation:bp1];
[queue addOperation:bp2];
[queue addOperation:bp3];
}
任务之间的依赖(NSOperation)