直接撸代码
- 看一下下面的输出log
现在一个页面有很多请求, 有的时候, 需要等请求全部完成,才能去刷新页面,这样就用到队列,GCD, 下面就是一个例子
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create(0, 0);
dispatch_group_enter(group);
dispatch_group_enter(group);
dispatch_group_enter(group);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1.0];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"任务1 完成");
dispatch_group_leave(group);
});
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2.0];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"任务2 完成");
dispatch_group_leave(group);
});
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3.0];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"任务3 完成");
dispatch_group_leave(group);
});
});
NSLog(@"开始执行");
dispatch_group_notify(group, queue, ^{
NSLog(@"全部完成");
});
NSLog(@"最后一步");
网友评论