Domanda

I'm developing an iPhone app for IOS5. I'm currently using the CLGeocoder class located within the CoreLocation framework. I cannot figure out if the completion handler block is called at the end, after the geocoding takes place or concurrently.

I only know that the completion handler block is ran on the main thread. Does anyone know if the completion handler block is ran on completion of the geocoded or is the code for completing the task at hand while the geocoder executes on another thread?

È stato utile?

Soluzione

The completion handler is run after the geocoder has finished geocoding. In other words, it's run on completion of the geocoding task. It's not for completing some other task while the geocoder runs.

The completion handler contains the placemarks and an error. If geocoding was successful, you get a placemarks array. If not, you get an error.

Notes from the docs:

This method submits the specified location data to the geocoding server asynchronously and returns. Your completion handler block will be executed on the main thread. After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.

Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. When the maximum rate is exceeded, the geocoder passes an error object with the value kCLErrorNetwork to your completion handler.

@interface MyGeocoderViewController ()

@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation MyGeocoderViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    // Create a geocoder and save it for later.
    self.geocoder = [[CLGeocoder alloc] init];
}
- (void)geocodeAddress:(NSString *)addressString
{
    // perform geocode
    [geocoder geocodeAddressString:addressString
        completionHandler:^(NSArray *placemarks, NSError *error) {

        if ((placemarks != nil) && (placemarks.count > 0)) {
            NSLog(@"Placemark: %@", [placemarks objectAtIndex:0]);
        }
        // Should also check for an error and display it
        else {
            UIAlertView *alert = [[UIAlertView alloc] init];
            alert.title = @"No places were found.";
            [alert addButtonWithTitle:@"OK"];
            [alert show];
        }
    }];
}

@end

Altri suggerimenti

The Apple documentation says: This method submits the specified location data to the geocoding server asynchronously and returns. Your completion handler block will be executed on the main thread. After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.

No need to use GCD to dispatch the update on an IBOulet.

http://developer.apple.com/library/ios/#DOCUMENTATION/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top