Question

I'm experiencing a weird issue with - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;.

I have a list of locations need to be looked up. So I did something like

for (Trip *trip in takenTrips) {
    [geoCoder geocodeAddressString:trip.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //handling result
    }];
}

But it seems the completion handler is only being called once. I tried making these lookups sequentially. Then everything works fine.

I searched around for a while, but couldn't find anything similar to this. I'm totally baffled here...

Any suggestion is welcomed!

Was it helpful?

Solution

Applications should be conscious of how they use geocoding. rules of thumb for using this class effectively: Send at most one geocoding request for any one user action. class link

To solve your problem you could the one of the following:

  1. Do-while loop that checks if geocoder is processing geocoding
  2. NSOperationQueue to excucute one at time if it complete
  3. in the completeHandle execute a callback that run the next geocode

OTHER TIPS

Why don't you use more geocoder?

dispatch_queue_t geocoderQueue = dispatch_queue_create("geocoder.queue", DISPATCH_QUEUE_CONCURRENT); 
dispatch_apply([takenTrips count], dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ull), ^(size_t index) {

    NSString *loc = [(Trip *)takenTrips[index] location];

    CLGeocoder *geoCoder = [CLGeocoder new];
    [geoCoder geocodeAddressString:loc completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks firstObject];
        NSLog(@"%@",placemark.postalCode);
    }];
});

and also in a private concurrent queue so you can have a great result in performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top