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) whileNSURLConnectionhas an asynchronous API. always use this asynchrounous API instead when you need to do any network request.This is better for many reasons:
MyGeocoderthat sends the request, handle the response, parse it, and return the value in an asynchronous way)stringWithContentsOfURL+ GCD) is thatNSURLConnectionis integrated with theNSRunLoopand 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).GCD is always better than using
NSThreadsdirectly, but using Runloop scheduling for things that are already made for in the SDK, especiallyNSURLConnectionsis 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
OHURLLoaderclass and use it this way: