使用调度队列的Google地理编码效率 - 如何改进 - iPhone [英] Efficiency of Google Geocoding with Dispatch Queue - How to Improve - iPhone

查看:63
本文介绍了使用调度队列的Google地理编码效率 - 如何改进 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用Google地图视图,该视图通过地理编码填充了针脚。我使用下面的代码创建一个调度队列,然后向Google查询我应用中每个位置的经度和纬度。



问题是,尽管下面的代码在某种程度上起作用,似乎错过了第一次运行中很大一部分项目。按照下面的代码将这些项目添加到阵列failedLoad。

目前,我正在运行第二种方法来添加在每次调用ViewDidLoad方法时调用的failedLoad中的位置。然而,这是一个糟糕的解决方案,因为即使在第二种方法运行之后,仍然存在failedLoad中的项目,并且我还希望所有引脚都可以在不依赖ViewDidLoad的情况下加载(只有在用户点击引脚后才会调用,然后返回从提出的详细视图屏幕)。



任何人都可以提出一个改进这个过程的好方法吗?

谢谢

   - (void)displayPlaces {$ b $ (PlaceObject * info in mapLocations){

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); b

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 *错误;

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 ([LIS tItems count]> = 4&& [[listItems objectAtIndex:0] isEqualToString:@200]){
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];


$ b else {

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

}

CLLocationCoordinate2D坐标;
coordinate.latitude =纬度;
coordinate.longitude = longitude;
MyLocation * annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
$ b $ dispatch_sync(dispatch_get_main_queue(),^ {

//添加注解
[mapViewLink addAnnotation:annotation];

});

});


解决方案

GCD很棒,但您应该如果SDK已经为此提供了一个异步API,那么从不使用线程技术。在你的情况下,不要使用 stringWithContentsOfURL: ,因为它是一个同步&阻止代码(这可能是为什么你切换到使用GCD在后台),而 NSURLConnection 有一个异步API。 当您需要执行任何网络请求时,请始终使用此异步API

这有以下几个原因更好:




  • 其中之一就是它已经在SDK中为此设计了一个API(即使您可能需要创建一个类,如 MyGeocoder 发送请求,处理响应,解析它,并以异步方式返回值)
  • ,但是选择异步API的最重要原因同步 stringWithContentsOfURL + GCD)是 NSURLConnection NSRunLoop 并在runloop上调度套接字数据,避免为此创建大量无用的线程(如果在严格需要的地方使用线程,则线程是邪恶的)。

  • 最后,由于NSURLConnection生命周期由RunLoop本身处理,委托方法已经在mai上调用


GCD总是优于直接使用 NSThreads ,但使用对于已经在SDK中完成的事情,特别是 NSURLConnections ,Runloop调度对于性能(避免线程调度问题),多线程问题以及更多问题总是更好。






b例如,要自己实现这个类,可以使用我的示例 OHURLLoader class and use this way:

  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 ...
//这个回调/块在主线程上执行,所以没有问题写在这里
[mapViewLink addAnnotation:annotation];
} errorHandler:^(NSError * error){
NSLog(@下载%@时出错:%@,url,error);
}];


I have a Google Map view in my app which is populated with pins via geocoding. 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.

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.

At the moment I am running a second method to add the places in failedLoad which is called whenever the ViewDidLoad method is called. 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).

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. 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. always use this asynchrounous API instead when you need to do any network request.

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)
  • 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).
  • And finally, as NSURLConnection lifecycle is handled by the RunLoop itself, the delegate methods are already called on the main thread.

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.


[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:

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);
}];

这篇关于使用调度队列的Google地理编码效率 - 如何改进 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆