我正在使用以下命令发起本地化请求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 个第一个位置更新后没有其他任何事情发生。
谢谢 !