我所做项目的需求是,当前页面有多个网络请求,等待所有网络请求结束后,拿到数据,刷新View
示例代码用 dispatch_after 当做是网络请求了
实现如下:
代码语言:javascript复制dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
NSLog(@"执行1");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_group_leave(group);
NSLog(@"完成1");
});
dispatch_group_enter(group);
NSLog(@"执行2");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_group_leave(group);
NSLog(@"完成2");
});
dispatch_group_enter(group);
NSLog(@"执行3");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
dispatch_group_leave(group);
NSLog(@"完成3");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"都完成后,执行");
});