1.使用NSOperation queue添加任务
第一种方式
第一种使用addOperationWithBlock直接向operation queue中添加一个closure,它会被自动转换成NSBlockOperation
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// 1.下载图片
NSURL *url = [NSURL URLWithString:@"http://imgsrc.baidu.com/imgad/pic/item/caef76094b36acaf0accebde76d98d1001e99ce7.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img1 = [UIImage imageWithData:data];
// 2.在主线程更新UI
[NSOperationQueue.mainQueue addOperationWithBlock:^{
_imagView.image = img1;
}];
}];
第二种方式
先创建一个NSBlockOperation对象,然后把它添加到operation queue。这样做的好处是,我们可以设置NSBlockOperation对象的completionBlock属性
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://img.ph.126.net/ocT0cPlMSiTs2BgbZ8bHFw==/631348372762626203.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img2 = [UIImage imageWithData:data];
// 2.在主线程更新UI
[NSOperationQueue.mainQueue addOperationWithBlock:^{
_imagView2.image = img2;
}];
}];
// 任务完成时进行打印
op2.completionBlock = ^{
NSLog(@"img2 downloaded");
};
[queue addOperation:op2];
3.设置任务之间的关联性
下面所示例子中op2依赖于op3,op3依赖于op4操作的完成,并将op2,op3,op4添加到queue中,执行下载操作
最后执行此方法[queue cancelAllOperations]可以取消操作:
1.所有已经完成的任务,取消操作不会有任何结果;
2.如果一个任务被取消,所有和它关联的任务也会被取消;
3.任务被取消之后,completionBlock仍旧会被执行
- (void)test{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://pic39.nipic.com/20140318/496038_202510516000_2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:data];
// 2.在主线程更新UI
[NSOperationQueue.mainQueue addOperationWithBlock:^{
_imagView.image = img;
}];
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/12/03/18/68w58PICjJP.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img2 = [UIImage imageWithData:data];
// 2.在主线程更新UI
[NSOperationQueue.mainQueue addOperationWithBlock:^{
_imagView2.image = img2;
}];
}];
NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSURL *url = [NSURL URLWithString:@"http://img.ph.126.net/ocT0cPlMSiTs2BgbZ8bHFw==/631348372762626203.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img3 = [UIImage imageWithData:data];
// 2.在主线程更新UI
[NSOperationQueue.mainQueue addOperationWithBlock:^{
_imagView3.image = img3;
}];
}];
[op2 addDependency:op3];
[op3 addDependency:op4];
[queue addOperation:op4];
[queue addOperation:op3];
[queue addOperation:op2];
//执行此方法可以取消
//[queue cancelAllOperations];
}
4、GCD详情
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 执行耗时的异步操作...
dispatch_async(dispatch_get_main_queue(), ^{
// 回到主线,程执行UI刷新操作
}
分别异步执行两个耗时操作,等两个操作都执行完毕后在回到主线程执行操作,使用队列组(dispatch_group_t)快速,高效实现上述需求
dispatch_group_t group = dispatch_group_create(); // 队列组
dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //全局并发队列
dispatch_group_async(group, queue, ^{
// 异步执行操作1
// longTime1
});
dispatch_group_async(group, queue, ^{
// 异步执行操作2
// longTime2
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 在主线程刷新数据
// reloadData
});
// 创建串行队列
dispatch_queue_t queue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);