1。监听一组任务是否完成,完成后得到通知,执行其他的操作
dispatch_group_create :创建
dispatch_group_async / dispatch_group_async_f :分组操作 (组里的任务是一个接一个的操作)
dispatch_group_notify (group , queue , block) :在组里的操作都结束时,接收通知
e.g.
1.dispatch_group_async
dispatch_group_t taskGroup = dispatch_group_create();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
//因为这里是更新界面,所以是在主线程
dispatch_group_async(taskGroup, mainQueue, ^{
[self reloadTableView];
});
dispatch_group_async(taskGroup, mainQueue, ^{
[self reloadScrollView];
});
dispatch_group_async(taskGroup, mainQueue, ^{
[self reloadImageView];
});
/* At the end when we are done, dispatch the following block */
dispatch_group_notify(taskGroup, mainQueue, ^{
/* Do some processing here */
[[[UIAlertView alloc] initWithTitle:@"Finished"
message:@"All tasks are finished"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
});
e.g.
2.C函数:dispatch_group_async_f
void reloadAllComponents(void *context){
AppDelegate *self = (__bridge AppDelegate *)context;
[self reloadTableView];
[self reloadScrollView];
[self reloadImageView];
}
- (void) actionGroup:(NSDictionary *)launchOptions{
dispatch_group_t taskGroup = dispatch_group_create();
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_group_async_f(taskGroup,
mainQueue,
(__bridge void *)self, //C函数中的参数
reloadAllComponents);
dispatch_group_notify(taskGroup, mainQueue, ^{
[[[UIAlertView alloc] initWithTitle:@"Finished"
message:@"All tasks are finished"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil] show];
});
postscript:
宏
NSLog(@"1-%s", __FUNCTION__); //当前函数签名
NSLog(@"2-%d",__LINE__); //在源代码文件中当前所在行数
NSLog(@"3-%s",__FILE__); //当前源代码文件全路径
关键字
__bridge : 在Core Foundation和ARC之间,做类型转换时用到