我想根据加速度计更新更新filterX
,但变量是nil
我构建时的一半时间。起初我认为这是self
的范围问题,但意识到它在另一半时间不起作用。
我的第二个猜测是,它是某种竞争条件。这既没有意义,因为代码是同步运行的,并且它应该自己纠正,因为父函数每0.1秒运行一次。
感谢任何帮助。
class LocationTrackingService: CLLocationManagerDelegate {
static let sharedInstance = LocationTrackingService()
private override init() {}
var locationManager = CLLocationManager()
var motionManager = CMMotionManager()
var filterX : KalmanFilter<Double>?
func startTracking() {
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.startUpdatingLocation()
motionManager.startAccelerometerUpdates()
let location = locationManager.location!
let XYCoord = LatLonToXY(coordinate: (location.coordinate))
self.filterX = KalmanFilter(stateEstimatePrior: XYCoord.x, errorCovariancePrior: pow(location.horizontalAccuracy, 2))
}
if motionManager.isAccelerometerAvailable {
print(self.filterX) // <-- prints object no problem
motionManager.accelerometerUpdateInterval = 0.1
motionManager.startAccelerometerUpdates(to: OperationQueue.main) { data, _ in
print(self.filterX) // <------- prints nil half the time (i.e. half of the builds)
if let acceleration = data?.acceleration {
if self.filterX != nil {
// do stuff
}
}
}
}
}
答案 0 :(得分:0)
您的locationManager(_:didChangeAuthorization:)
委托方法在哪里?在继续startUpdatingLocation()
之前,您需要获得授权。