我关注 RAY WENDERLICH GCD tutorial- part 2 ,我不明白:
首次实现
- (void)downloadPhotosWithCompletionBlockBatchPhotoDownloadingCompletionBlock)completionBlock
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1
__block NSError *error;
dispatch_group_t downloadGroup = dispatch_group_create(); // 2
for (NSInteger i = 0; i < 3; i++) {
NSURL *url;
switch (i) {
case 0:
url = [NSURL URLWithString:kOverlyAttachedGirlfriendURLString];
break;
case 1:
url = [NSURL URLWithString:kSuccessKidURLString];
break;
case 2:
url = [NSURL URLWithString:kLotsOfFacesURLString];
break;
default:
break;
}
dispatch_group_enter(downloadGroup); // 3
Photo *photo = [[Photo alloc] initwithURL:url
withCompletionBlock:^(UIImage *image, NSError *_error) {
if (_error) {
error = _error;
}
dispatch_group_leave(downloadGroup); // 4
}];
[[PhotoManager sharedManager] addPhoto:photo];
}
dispatch_group_wait(downloadGroup, DISPATCH_TIME_FOREVER); // 5
dispatch_async(dispatch_get_main_queue(), ^{ // 6
if (completionBlock) { // 7
completionBlock(error);
}
});
});
}
第二次实现:
- (void)downloadPhotosWithCompletionBlockBatchPhotoDownloadingCompletionBlock)completionBlock
{
// 1
__block NSError *error;
dispatch_group_t downloadGroup = dispatch_group_create();
for (NSInteger i = 0; i < 3; i++) {
NSURL *url;
switch (i) {
case 0:
url = [NSURL URLWithString:kOverlyAttachedGirlfriendURLString];
break;
case 1:
url = [NSURL URLWithString:kSuccessKidURLString];
break;
case 2:
url = [NSURL URLWithString:kLotsOfFacesURLString];
break;
default:
break;
}
dispatch_group_enter(downloadGroup); // 2
Photo *photo = [[Photo alloc] initwithURL:url
withCompletionBlock:^(UIImage *image, NSError *_error) {
if (_error) {
error = _error;
}
dispatch_group_leave(downloadGroup); // 3
}];
[[PhotoManager sharedManager] addPhoto:photo];
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{ // 4
if (completionBlock) {
completionBlock(error);
}
});
}
我是第一个实现,相关代码被dispatch_async 包围,一切都 super 清晰。
但是,第二个实现还不清楚!我不明白,GCD 机制除了进入和离开通知之外是如何参与的?
Best Answer-推荐答案 strong>
第一个实现从 开始运行一个后台线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ // 1
所以整个函数在那里运行。但是这个后台线程最终在这一行被阻塞(它等待所有照片被下载):
dispatch_group_wait(downloadGroup, DISPATCH_TIME_FOREVER);
这一切都很好,因为您没有阻塞主线程。但这是一个有点笨拙的实现,因为,
- 你阻塞了一个你确实不需要的线程并且
- 整个函数周围的
dispatch_async 代码看起来有点难看。
所以第二种实现有两个好处:
- 它不是那么“丑陋”并且更具可读性(因为您摆脱了
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{} )
- 并且没有阻塞,因为
dispatch_group_notify 充当异步完成 block 。
关于ios - GCD机制在哪里调用?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/25303525/
|