gpt4 book ai didi

multithreading - CLLocationmanager从后台线程更新

转载 作者:行者123 更新时间:2023-12-01 19:28:51 30 4
gpt4 key购买 nike

我正在使用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方法中, CLLocationManagerLocationManagerController属性被初始化并要求输入 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个位置更新后没有其他 react 。

谢谢 !

最佳答案

从CLLocationManager类引用:

Configuration of your location manager object must always occur on a thread with an active run loop, such as your application’s main thread.

关于multithreading - CLLocationmanager从后台线程更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4631605/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com