简体   繁体   English

带有调度队列的Google地理编码的效率-如何提高-iPhone

[英]Efficiency of Google Geocoding with Dispatch Queue - How to Improve - iPhone

I have a Google Map view in my app which is populated with pins via geocoding. 我的应用程序中有一个Google Map视图,该视图通过地理编码填充了图钉。 I am using the below code to create a dispatch queue which then queries Google for the longitude and latitude for each place in my app. 我正在使用以下代码创建一个调度队列,然后查询Google应用中每个位置的经度和纬度。

The problem is that although the below code works to some extent, it seems to miss a large percentage of items on its first run through. 问题是,尽管以下代码在某种程度上可以正常工作,但它似乎在第一次运行时会丢失很大一部分项目。 These items are added to the array "failedLoad" as per the code below. 根据以下代码,将这些项目添加到数组“ failedLoad”中。

At the moment I am running a second method to add the places in failedLoad which is called whenever the ViewDidLoad method is called. 目前,我正在运行第二种方法来添加failedLoad中的位置,每当调用ViewDidLoad方法时都会调用该方法。 However this is a poor solution as even after this second method is run there are still items in failedLoad and also I would prefer for all the pins to load without relying on ViewDidLoad (which is only called if the user taps on a pin, then returns from the detail view screen which is presented). 但是,这是一个糟糕的解决方案,因为即使在运行第二种方法之后,failedLoad中仍然有项目,而且我更希望所有引脚都加载而不依赖ViewDidLoad(仅当用户点击某个引脚然后返回时才调用从显示的详细信息视图屏幕中)。

Can anyone suggest a good way of improving this process ? 任何人都可以提出改进此过程的好方法吗?

Thank you 谢谢

-(void)displayPlaces {

for (PlaceObject *info in mapLocations) {

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^

   {

        // GET ANNOTATION INFOS
        NSString * addressOne = info.addressOne;
        NSString * name = info.name;
        NSString * postCode = info.postCode;

        NSString * addressTwo = [addressOne stringByAppendingString:@",London,"];
        NSString * address = [addressTwo stringByAppendingString:postCode];

        NSError * error;

        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
        NSArray *listItems = [locationString componentsSeparatedByString:@","];

        double latitude = 0.0;
        double longitude = 0.0;

        if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
            latitude = [[listItems objectAtIndex:2] doubleValue];
            longitude = [[listItems objectAtIndex:3] doubleValue];

        } 

        else {

            NSLog(@"Error %@",name);
            [failedLoad addObject : info];

        }        

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude;
        coordinate.longitude = longitude;
        MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];

        dispatch_sync(dispatch_get_main_queue(), ^{

            // ADD ANNOTATION
            [mapViewLink addAnnotation:annotation];

       });

    });
}   

GCD is great but you should never use threading techniques if the SDK already offers an asynchronous API for this. GCD很棒,但是如果SDK已经为此提供了异步API,则永远不要使用线程技术。 In your case, never use stringWithContentsOfURL: as it is a synchronous & blocking code (which is probably why you switch to using GCD to make it in the background) while NSURLConnection has an asynchronous API. 在您的情况下, 切勿使用stringWithContentsOfURL:因为它是同步和阻止代码(这可能是为什么您切换到使用GCD在后台进行编码)的原因,而NSURLConnection具有异步API。 always use this asynchrounous API instead when you need to do any network request . 当您需要执行任何网络请求时,请始终使用此异步API

This is better for many reasons: 由于许多原因,这样做更好:

  • one being that it's an API already designed for this in the SDK (even if you will probably need to create a class like MyGeocoder that sends the request, handle the response, parse it, and return the value in an asynchronous way) 一个是因为它是SDK中已为此目的设计的API(即使您可能需要创建类似MyGeocoder的类来发送请求,处理响应,解析并以异步方式返回值)
  • but the most significant reason to prefer the asynchronous API (over using the synchronous stringWithContentsOfURL + GCD) is that NSURLConnection is integrated with the NSRunLoop and schedules the retrieval of the socket data on the runloop , avoiding to create a lot of useless threads for that (and threads are evil if you use it where it is not strictly needed). 但是,首选异步API(而不是使用同步stringWithContentsOfURL + GCD)的最重要的原因是NSURLConnectionNSRunLoop集成在一起,并安排在NSRunLoop上检索套接字数据,从而避免为此创建许多无用的线程(如果在严格不需要的地方使用线程,线程就是邪恶的。
  • And finally, as NSURLConnection lifecycle is handled by the RunLoop itself, the delegate methods are already called on the main thread. 最后,由于NSURLConnection生命周期由RunLoop本身处理,因此委托方法已经在主线程上调用。

GCD is always better than using NSThreads directly, but using Runloop scheduling for things that are already made for in the SDK, especially NSURLConnections is always better both for performance (avoid thread scheduling issues), multithreading issues and much more. GCD总是比直接使用NSThreads更好,但是将Runloop调度用于SDK中已实现的功能,尤其是NSURLConnections总是在性能(避免线程调度问题),多线程问题等方面都更好。


[EDIT] For example if you don't want to implement the class yourself, you can use my sample OHURLLoader class and use it this way: [编辑]例如,如果您不想自己实现该类,则可以使用我的示例OHURLLoader类,并通过以下方式使用它:

NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* req = [NSURLRequest requestWithURL:url];

OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithCompletion:^(NSData* receivedData, NSInteger httpStatusCode) {
    NSString* locationString = loader.receivedString;
    NSArray *listItems = [locationString componentsSeparatedByString:@","];
    ... etc ...
    // this callback / block is executed on the main thread so no problem to write this here
    [mapViewLink addAnnotation:annotation];
} errorHandler:^(NSError *error) {
    NSLog(@"Error while downloading %@: %@",url,error);
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM