myLocality と myCountry という 2 つのグローバル NSString 変数があります...
以下のように GeoCode を設定しました。
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
finishedGettingLocation = false;
if (newLocation.horizontalAccuracy < 200.0f){
[locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
__block NSString *locality;
__block NSString *country;
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
locality = placemark.locality;
country = placemark.ISOcountryCode;
}
}];
do {
} while ([geoCoder isGeocoding]);
myLocality = locality;
myCountry = country;
finishedGettingLocation = true;
}
}
そして、私の背景スレッドは次のとおりです。
- (void) doWork
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
finishedGettingLocation = false;
dispatch_async(dispatch_get_main_queue(), ^{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager startUpdatingLocation];
});
int counter = 0;
do {
[NSThread sleepForTimeInterval:.1];
counter ++;
if (counter % 200 == 0){
dispatch_async(dispatch_get_main_queue(), ^{
Toast *oToast = [Toast toastWithMessage:@"Gps pin-pointing works faster if you are outside"];
[oToast showOnView:self.view];
});
counter = 0;
}
} while (!finishedGettingLocation);
NSLog(@"%@", myCountry); // this is where I am trying to print
NSLog(@"%@", myLocality); // this is where I am trying to print
});
}
(ええ、私はそれがかなりずさんなコードであることを知っていますが、まあ.. xD)
今、私が理解できないのは、他の場所のコードにログを記録すると、これらが null を返す理由です。ただし、上記にログを入れるとfor
、つまりNSLog(@"%@", placemark.locality)
、正しい値が得られるか、少なくとも値が得られます。
からグローバル変数を設定するにはどうすればよいplacemark
ですか?