I have a feeling that my problem here is really with blocking, but maybe it's something else too. I am trying to forward geocode an address and place the coordinates into an array to use later.
An exception is raised down at the bottom when I try to call on one of the objects I tried added to the array in the block. The exception also gets raised before any of the NSLogs ever print out within the block text.
What's the proper way to handle this? Thanks.
- (NSMutableArray *)convertAddressToGeocode:(NSString *)addressString
{
//return array with lat/lng
__block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString
completionHandler:^ (NSArray* placemarks, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
if (error){
NSLog(@"Geocode failed with error: %@", error);
[self displayError:error];
return;
}
else {
NSArray const *keys = @[@"coordinate.latitude",
@"coordinate.longitude",
@"altitude",
@"horizontalAccuracy",
@"verticalAccuracy",
@"course",
@"speed",
@"timestamp"];
NSString *keylat = keys[0];
NSString *keylng = keys[1];
if (aPlacemark.location == nil)
{
NSLog(@"location is nil.");
}
else if ([keylat isEqualToString:@"coordinate.latitude"] && [keylng isEqualToString:@"coordinate.longitude"])
{
NSString *lat = @"";
NSString *lng = @"";
lat = [self displayStringForDouble: [aPlacemark.location coordinate].latitude];
lng = [self displayStringForDouble: [aPlacemark.location coordinate].longitude];
NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED
[coordinates addObject:[NSString stringWithFormat:@"%@",lat]];
[coordinates addObject:[NSString stringWithFormat:@"%@",lng]];
}}}}];
NSLog(@"Array: %@", coordinates[0]); //EXCEPTION RAISED HERE, Nothing ever gets added
return coordinates;
}
Here is the code this method is supposed to be plugged into, but I'm not getting the coordinates out of convertAddresstoGeocode to pass to convertCoordinatestoRepModel:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *addressToSearch = self.addressSearchField.text;
NSMutableArray *coordinateArray = [self convertAddressToGeocode:addressToSearch];
NSMutableArray *repModelArray = [self convertCoordinatesToRepModel:coordinateArray];
...
}
The problem is here; just replace the above code with:
if
geocodeAddressString
is async operation than your block will be performed afteralso, after call of your method ends (when event already handled) the coordinates array released (it is because of __block modifier - blocks do not retain objects with __block modifier), and in your block you try to use dealloced
coordinates
array.Once again:
Your block will be called after
NSLog(@"Array: %@", coordinates[0]);
f.e.:
NSLog(@"Array: %@", coordinates[0]);
- it is normal that in that moment array is empty.coordinates
array in some@property
, you can release it after using in blockUPDATE:
in .h file
under
@intrerface
in .m file
That should works!