等待CLGeocoder完成并发枚举

编程入门 行业动态 更新时间:2024-10-26 08:32:45
本文介绍了等待CLGeocoder完成并发枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在类方法中有以下代码

I have the following bit of code in a class method

NSDictionary *shopAddresses = [[NSDictionary alloc] initWithContentsOfFile:fileName]; NSMutableArray *shopLocations = [NSMutableArray arrayWithCapacity:shopAddresses.count]; [shopAddresses enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, ShopLocation *shopLocation, BOOL *stop) { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:shopLocation.address completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"Geocode failed with error: %@", error); } else { shopLocation.placemark = [placemarks objectAtIndex:0]; } [shopLocations addObject:shopLocation]; }]; }

执行此代码后,我想返回shopLocations数组作为结果对于该方法。但是,如果我不希望数组为空,我需要等待所有地理编码器搜索完成。

After execution of this code, I want to return the shopLocations array as a result for the method. However I need to somehow wait until all geocoder searches have finished if I don't want the array to be empty.

我该怎么做?

我尝试了不同的GCD方法,但没有成功远。

I have tried different GCD approaches, but haven't been successful so far.

推荐答案

这可以通过 dispatch_group _... 函数来处理:

This can be handled by the dispatch_group_... functions:

… dispatch_group_t group = dispatch_group_create(); [shopAddresses enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop) { dispatch_group_enter(group); CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:shopLocation.address completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"Geocode failed with error: %@", error); } else { shopLocation.placemark = [placemarks objectAtIndex:0]; } [shopLocations addObject:shopLocation]; dispatch_group_leave(group); }]; }]; while (dispatch_group_wait(group, DISPATCH_TIME_NOW)) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.f]]; } dispatch_release(group); …

我正在使用这些块积累一些网络请求。

I'm using these kind of blocks to accumulate some network requests.

我希望这可以提供帮助。

I hope this can help.

更多推荐

等待CLGeocoder完成并发枚举

本文发布于:2023-11-10 01:54:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CLGeocoder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!