iphoneobjective-ciosmkmapviewmkreversegeocoder

Is MKReverseGeocoder delegate method working on separate thread?


I am trying draw route between two location.For that i have two instance of CLLocation startPoin,endPoind. I want to convert this two location two corresponding Location Address.After that i want to fetch all route points from Google map webservice. So did the following code.

- (void)viewDidLoad{
    [super viewDidLoad];
    locations =[[NSMutableArray alloc]initWithCapacity:2];//For location Name 
    [self.mapView_MP setMapType:MKMapTypeStandard];
    [self.mapView_MP setZoomEnabled:YES];
    [self.mapView_MP setScrollEnabled:YES];
    [self update:startPoint];  //MKReverseGeocoder start point  
    [self update:endPoint];    //MKReverseGeocoder end point

    [self updateRoute];

}

- (void)update:(CLLocation*)loc{
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:loc.coordinate];


    geoCoder.delegate = self;
    [geoCoder start];

}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{

    NSMutableDictionary *cplace=[[NSMutableDictionary alloc]initWithDictionary:[placemark addressDictionary]];

   // NSLog(@"The geocoder has returned: %@", [cplace objectForKey:@"FormattedAddressLines"] );
   // NSLog(@"The geocoder has returned: %@", [cplace objectForKey:@"Street"] );
    [locations addObject:[cplace objectForKey:@"SubAdministrativeArea"]];
     NSLog(@"The geocoder has returned: %@", locations );
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)updateRoute {

    self.responseData=[NSMutableData data];  
    NSLog(@" string Formate.................................===%@",locations);   

    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&mode=driving&sensor=false", [locations objectAtIndex:0],[locations objectAtIndex:1]]]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];    
}

In viewDidLoad i am calling the methods in the order

[self update:startPoint];      //MKReverseGeocoder start point  
    [self update:endPoint];    //MKReverseGeocoder end point           
    [self updateRoute];

but unfortunately [self updateRoute]; is executing first compare to MKReverseGeocoder delegate method. there for the location array is null in side this method. Getting EXEBADACCESS. how can i over come this value. Inside MKReverseGeocoder delegate method i got location name. the Delegate methods is executing any other thread.


Solution

  • MKReverseGeocoder is deprecated in iOS 5, so you should probably use CLGeocoder instead.

    That aside, yes, the start method is asynchronous, as indicated in the documentation. That doesn't necessarily mean that you'll get the results on a different thread, but it means that the method will usually return before results are available.

    You need to keep track of your geocoders to call your updateRoute method when both have finished.