Multithreading CLLocation manager从后台线程更新
我正在使用Multithreading CLLocation manager从后台线程更新,multithreading,ios,cllocationmanager,grand-central-dispatch,Multithreading,Ios,Cllocationmanager,Grand Central Dispatch,我正在使用grandcentraldispatch启动本地化请求: - (void) findGroceriesNearMe { dispatch_queue_t downloadQueue = dispatch_queue_create("Groceries downloader", NULL); dispatch_async(downloadQueue, ^{ CLLocationCoordinate2D userLocation = [LocationMan
grandcentraldispatch
启动本地化请求:
- (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控制器中调用静态方法:
+ (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方法中,初始化LocationManagerController
的属性CLLocationManager
,并要求其执行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];
}
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
{
位置拒绝=否;
NSLog(@“%f,%f”,newLocation.coordinate.latitude,newLocation.coordinate.longitude);
NSDate*eventDate=newLocation.timestamp;
NSTimeInterval howRecent=[eventDate timeIntervalSinceNow];
//关于新地点的维里菲·奎拉(vérifie que la newLocation est récente)
如果(abs(最近更新)>10.0){
返回;
}
//测试是否为无效测量值
if(newLocation.horizontalcreaction<0)返回;
//测试测量值,看它是否符合要求的精度
如果(newLocation.horizontalAccuracy猜测。如果你坐在办公桌旁,用模拟器进行测试,精确度可能不会比你想要的更好
if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
if(CLLocationManager类参考中的newLocation.horizontalAccuracy)
位置管理器对象的配置必须始终在
具有活动运行循环(如应用程序的主循环)的线程
线
我看不出有什么理由需要使用GCD来实现此目的?无论如何,位置管理器是异步的……你完全正确,mackross!这真是一个奇怪的设计想法!谢谢你让我回到正确的方向:)我实际上是在设备上测试的。我不认为问题来自这里,因为位置经理不会停止发送更新…mackross的评论非常相关:我不应该使用GCD来做这样的事情!我该如何设置其中一个呢?