转自:http://blog.csdn.net/liuyinghui523/article/details/50618092
Dispatch Group
在追加到Dispatch Queue中的多个任务处理完毕之后想执行结束处理,这种需求会经常出现。如果只是使用一个Serial Dispatch Queue(串行队列)时,只要将想执行的处理全部追加到该串行队列中并在最后追加结束处理即可,但是在使用Concurrent Queue 时,可能会同时使用多个Dispatch Queue时,源代码就会变得很复杂。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create(
"com.gcd-group.www"
, DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
for
(
int
i = 0; i < 1000; i++) {
if
(i == 999) {
NSLog
(@
"11111111"
);
}
}
});
dispatch_group_async(group, queue, ^{
NSLog
(@
"22222222"
);
});
dispatch_group_async(group, queue, ^{
NSLog
(@
"33333333"
);
});
dispatch_group_notify(group, queue, ^{
NSLog
(@
"done"
);
});
|
控制台的输出:
因为向Concurrent Dispatch Queue 追加处理,多个线程并行执行,所以追加处理的执行顺序不定。执行顺序会发生变化,但是此执行结果的done一定是最后输出的。
无论向什么样的Dispatch Queue中追加处理,使用Dispatch Group都可以监视这些处理执行的结果。一旦检测到所有处理执行结束,就可以将结束的处理追加到Dispatch Queue中,这就是使用Dispatch Group的原因。
下面试一个使用Dispatch Group异步下载两张图片,然后合并成一张图片的medo(注意,我们总是应该在主线程中更新UI):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#import "ViewController.h"
@interface
ViewController ()
@property
(
nonatomic
, strong) UIImage *imageOne;
@property
(
nonatomic
, strong) UIImage *imageTwo;
@property
(
nonatomic
, weak) UILabel *textLabel;
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super
viewDidLoad];
[
self
operation1];
}
- (
void
)operation1
{
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 450, 0, 0)];
textLabel.text = @
"正在下载图片"
;
[textLabel sizeToFit];
[
self
.view addSubview:textLabel];
self
.textLabel = textLabel;
[
self
group];
NSLog
(@
"在下载图片的时候,主线程貌似还可以干点什么"
);
}
- (
void
)group
{
UIImageView *imageView = [[UIImageView alloc] init];
[
self
.view addSubview:imageView];
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create(
"cn.gcd-group.www"
, DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
NSLog
(@
"正在下载第一张图片"
);
NSData
*data = [
NSData
dataWithContentsOfURL:[
NSURL
URLWithString:@
"http://images2015.cnblogs.com/blog/471463/201509/471463-20150912213125372-589808688.png"
]];
NSLog
(@
"第一张图片下载完毕"
);
self
.imageOne = [UIImage imageWithData:data];
});
dispatch_group_async(group, queue, ^{
NSLog
(@
"正在下载第二张图片"
);
NSData
*data = [
NSData
dataWithContentsOfURL:[
NSURL
URLWithString:@
"http://images2015.cnblogs.com/blog/471463/201509/471463-20150912212457684-585830854.png"
]];
NSLog
(@
"第二张图片下载完毕"
);
self
.imageTwo = [UIImage imageWithData:data];
});
dispatch_group_notify(group, queue, ^{
UIGraphicsBeginImageContext(CGSizeMake(300, 400));
[
self
.imageOne drawInRect:CGRectMake(0, 0, 150, 400)];
[
self
.imageTwo drawInRect:CGRectMake(150, 0, 150, 400)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
[
self
.view addSubview:imageView];
self
.textLabel.text = @
"图片合并完毕"
;
});
});
}
@end
|