简体   繁体   English

在NSTimer中使用startMonitoringSignificantLocationChanges可以提高电池使用率

[英]Using startMonitoringSignificantLocationChanges with NSTimer to improve battery usage

I'm currently working in an iOS app that other developer started. 我目前正在开发其他开发人员的iOS应用程序。 The app needs to monitor location changes because it needs to know the user position with low precision (hundred meters). 该应用程序需要监视位置更改,因为它需要以低精度(百米)知道用户位置。 The previous implementation of the location stuff was done using an NSTimer and startUpdatingLocation . 之前的位置内容实现是使用NSTimerstartUpdatingLocation The execution goes like this: 执行如下:

// Fire each 10 seconds start updating location
self.timerPosition = [NSTimer scheduledTimerWithTimeInterval:ti
                                                      target:self
                                                    selector:@selector(location)
                                                    userInfo:nil
                                                     repeats:YES];
[self.timerPosition fire];

Location selector does this 位置选择器执行此操作

// Configure & check location services enabled
...
self.locman.delegate = self;
self.locman.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locman startUpdatingLocation];

And then in the location manager delegate 然后在位置经理代表处

[manager stopUpdatingLocation];

But reading about getting the user location in Apple docs, it seems that the right way to get the location with low-power consumption is to use startMonitoringSignificantLocationChanges . 但是阅读有关获取Apple文档中的用户位置的信息,似乎以低功耗获取位置的正确方法是使用startMonitoringSignificantLocationChanges

My question is, is a good decision to keep the location timer in combination with startMonitoringSignificantLocationChanges instead of startUpdatingLocation , or it's a nonsense approach? 我的问题是,将位置计时器与startMonitoringSignificantLocationChanges结合使用而不是startUpdatingLocation是一个很好的决定,还是一个无意义的方法?

I do not need to get location when the app is in the background, but I want to know when the user has changed it's position when the app is active. 当应用程序在后台时,我不需要获取位置,但我想知道用户在应用程序处于活动状态时何时更改了它的位置。

I can tell you that the timer can't and won't be needed when you are using the low-power -startMonitoringSignificantLocationChanges. 我可以告诉你,当你使用低功率-startMonitoringSignificantLocationChanges时,不能也不需要定时器。 That method only responds to callbacks from the delegate when the device detects a change. 该方法仅在设备检测到更改时响应委托的回调。 This check for location is not using GPS, it uses Wifi and cell-tower triangulation which is already happening. 这个位置检查不使用GPS,它使用已经发生的Wifi和蜂窝塔三角测量。 So by using this method, there is no need to slow it down to save battery life. 因此,通过使用此方法,无需减慢速度以节省电池寿命。 Just set up the delegate methods and respond accordingly. 只需设置委托方法并做出相应的响应。

I'm not sure what your implementation of location is for, but the region monitoring is also another great way to get location updates using little to no battery. 我不确定您的位置实现是什么,但区域监控也是使用很少甚至没有电池来获取位置更新的另一种好方法。 Region monitoring is much more helpful when you have specific locations to monitor rather than just general user location. 当您具有要监视的特定位置而不仅仅是一般用户位置时,区域监视会更有帮助。 Hope this clears things up. 希望这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM