The reverseGeocodeLocation
method runs asynchronously, so you shouldn't reference the annotation
outside of the completionHandler
block. The annotation
string has not been set by the point you get to that code after the reverseGeocodeLocation
block.
Thus, you might want to move your code after the reverseGeocodeLocation
block to inside it, resolving this asynchronous issue:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSLog(@"location %@",addressLabel);*/
NSString *annotation = [NSString stringWithFormat:@" %@, %@, %@",
placemark.locality,
placemark.administrativeArea,
placemark.country];
NSLog(@"location %@",annotation);
NSString *text = self.toPostCell.postText.text;
UIImage *image = self.toPostCell.selectedImage.image;
[Persistence addPostToServerAddtext:text addimage:image addbeach:nil location:annotation];
// I'm not sure why you're sleeping 5 seconds, but if you really want to do
// something five seconds later you should probably `dispatch_after`, thus
// instead of:
//
// [NSThread sleepForTimeInterval:5.0];
//
// you might want to do the following:
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
self.posts = [Persistence getPostsUpTo: self.cantPosts forBeach:nil];
imageLoaded = NO;
[self.tableView reloadData];
});
}
} ];
}