gpt4 book ai didi

ios - didUpdateLocations 每秒通过延迟更新不断调用

转载 作者:可可西里 更新时间:2023-11-01 06:17:12 28 4
gpt4 key购买 nike

我正在尝试实现延迟位置更新以获得更好的电池消耗。我正在这样启动我的位置管理器:

- (void)initCoreLocation
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;

//Très important pour iOS9 !
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
self.locationManager.allowsBackgroundLocationUpdates=YES;
}

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}

[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}

然后像这样开始延迟更新:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

if (!self.deferringUpdates) {
[self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:30];
self.deferringUpdates = YES;
}
}

-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error { // Stop deferring updates
if(error) {
NSLog(@"error");
}
NSLog(@"didFinishDeferredUpdates");
self.deferringUpdates = NO;
}

我每 30 秒记录一次 didFinishDeferredUpdates,但是 didUpdateLocations 每秒都在调用,消除了任何优化电池消耗的尝试。是否假设位置管理器每 30 秒调用一次 didUpdateLocations

最佳答案

也许您没有采用正确的方法,因为 allowDeferredLocationUpdatesUntilTraveled() 告诉 GPS 硬件在内部存储新位置,直到满足指定的距离或超时条件。

来自 iOS 开发者库:

If your app is in the foreground, the location manager does not defer the deliver of events but does monitor for the specified criteria. If your app moves to the background before the criteria are met, the location manager may begin deferring the delivery of events. Link

关于ios - didUpdateLocations 每秒通过延迟更新不断调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822590/

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