以下を使用してローカリゼーション リクエストを開始していGrand Central Dispatch
ます。
- (void) findGroceriesNearMe {
dispatch_queue_t downloadQueue = dispatch_queue_create("Groceries downloader", NULL);
dispatch_async(downloadQueue, ^{
CLLocationCoordinate2D userLocation = [LocationManagerController findMeWithCaller:self];
dispatch_async(dispatch_get_main_queue(), ^{
[self userSuccessFullyFound:userLocation];
});
});
dispatch_release(downloadQueue);
}
私の Singleton クラス LocationManager Controller で静的メソッドを呼び出します。
+ (CLLocationCoordinate2D) findMeWithCaller: (UIViewController *) viewController {
LocationManagerController *locationManagerController = [LocationManagerController locationManagerController];
[locationManagerController startUpdates];
while(![locationManagerController getterDone]){
//mystique pour nous-- a approfondir
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
startUpdates メソッドではCLLocationManager
、 のプロパティLocationManagerController
が初期化され、要求されstartUpdatingLocation
ます。
最後に、場所の更新が発生したときの方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locationDenied = NO;
NSLog(@"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
// On vérifie que la newLocation est récente
if (abs(howRecent) > 10.0) {
return;
}
// Test if it's not an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
// Test the measurement to see if it meets the desired accuracy
if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
{
latitude = newLocation.coordinate.latitude;
longitude = newLocation.coordinate.longitude;
locationDefined = YES;
[self setterDone:YES];
}
}
私の問題は、locationManager が 3 つの場所の更新のみを送信し、停止するように要求していないにもかかわらず、更新の送信を停止することです。基本的に、while(![locationManagerController getterDone]) ループから抜け出すことはありません。
ところで、GCD を使用してこれを実装しようとする前は問題なく動作していたので、問題はマルチスレッドの実装に関係していると思います。
何か案が ?
編集
コンソールにエラーは表示されません。プログラムはそのまま実行されますが、while ループに陥ってしまい、最初の 3 回の場所の更新後に何も起こりません。
ありがとう !