Ios 为什么在stopLocationManager之后调用UpdateLocations
我正在做一个iOS应用程序来关联用户的位置 我设置了CLLocationManager,它运行良好,但在我对CLLocationManager调用StopUpdateLocation()之后,委托方法“didUpdateLocations”仍然会被调用几次。这种情况并不总是发生 这是因为调用stopUpdateLocation()和locationManager之间的延迟真的停止了吗 以下是我的CLLocationManager的设置方式:Ios 为什么在stopLocationManager之后调用UpdateLocations,ios,swift,cllocationmanager,Ios,Swift,Cllocationmanager,我正在做一个iOS应用程序来关联用户的位置 我设置了CLLocationManager,它运行良好,但在我对CLLocationManager调用StopUpdateLocation()之后,委托方法“didUpdateLocations”仍然会被调用几次。这种情况并不总是发生 这是因为调用stopUpdateLocation()和locationManager之间的延迟真的停止了吗 以下是我的CLLocationManager的设置方式: final class LocationControl
final class LocationController: NSObject {
typealias Action = (CLLocation) -> ()
static let shareInstance = LocationController()
private let locationManager = CLLocationManager()
private var location: CLLocation?
private var action: Action?
private var timer: NSTimer?
private override init () {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.activityType = .Fitness
}
}
这是我的授权方式:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let accuracy = newLocation.horizontalAccuracy
// horizontalAccuracy less than 0 is invalid result, ignore it
guard accuracy >= 0 else { return }
// ignore the result that accuracy less than desiredAccuracy
guard accuracy <= locationManager.desiredAccuracy else { return }
// accept the result
self.location = newLocation
// stop locating
self.stopLocationManager()
}
这就是我使用LocationManager的方式:
func stopLocationManager() {
self.locationManager.stopUpdatingLocation()
print("the location manager should be stopped!!!!!!!!!!!")
if let timer = self.timer {
timer.invalidate()
}
guard let action = self.action else { return }
if let location = self.location {
action(location)
} else {
action(TomoConst.Geo.CLLocationTokyo)
}
self.action = nil
self.location = nil
}
func doActionWithLocation(action: Action) {
self.action = action
guard self.determineStatus() else { return }
self.timer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: Selector("stopLocationManager"), userInfo: nil, repeats: false)
self.locationManager.startUpdatingLocation()
print("the location manager started!!!!!!!!!!!")
}
结果,我看到“位置管理器启动!”一次,然后看到“位置管理器应该停止!”多次。我认为停止信息应该只打印一次。我调试了很长一段时间,没有运气
请告诉我为什么
提前谢谢
顺便说一句,如果我以一种非常简单的方式设置所有内容,LocationManager会正确停止:
let locman = CLLocationManager()
var count = 1
override func viewDidLoad() {
super.viewDidLoad()
locman.delegate = self
locman.desiredAccuracy = kCLLocationAccuracyHundredMeters
locman.activityType = .Fitness
locman.requestWhenInUseAuthorization()
locman.startUpdatingLocation()
print("start")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("\(count) \(locations.last)")
if self.count == 5 {
locman.stopUpdatingLocation()
}
count++
}
上面的代码将消息打印5次,并且似乎没有调用进一步的didUpdateLocations。是的,这是常见的行为。例如,虽然有其他问题,但即使你考虑到他停止位置服务的地点选择不当,你也会看到与你描述的相同的行为。检查您的自定义
location
属性是否为non-nil
将解决此问题,使用适当的distanceFilter
也可以解决此问题。哦,是的,我在未使用授权时调用了RequestWhenUseAuthorization并正确获得授权。LocationManager是以冷却方式停止的吗?添加带有时间戳的日志可以让我们更好地了解我们正在谈论的内容,例如,如果位置更新立即到达那里(即,在停止location manager之前,位置更新已经排好队,之后才交付),或者如果我们正在谈论更长的延迟。。。“locationManager是否以冷却方式停止?”…“这是因为调用StopUpdateLocation()和locationManager之间的延迟真的停止了吗?" ... 我不确定我们是否知道位置经理在内部做什么,但这并不重要。简单而实际的情况是,如果您在didUpdateLocations
中停止它(尤其是第一次调用),可能会发生一些对didUpdateLocations
的额外调用,您只需处理它。感谢您的提示,Rob。我想我必须接受现实:)。