I am trying to detect when a user starts driving (or being driven) while the app is in the background. The update rate of activity becomes very low while the app is background and this causes the app to miss the beginning of the drive. When the app is in the foreground, this problem does not exist.
Is there a way to remedy this?
Here is my code:
func startMotionClassification() {
guard CMMotionActivityManager.isActivityAvailable() else {
return
}
opQueue = {
let o = OperationQueue()
o.name = "core-motion-updates"
return o
}()
self.motionActivityManager.startActivityUpdates(to: opQueue!) { activity in
guard let activity = activity else {return}
if (activity.automotive || Constants.IS_DEBUG) &&
(activity.confidence == .medium || activity.confidence == .high) &&
!self.isDriving {
self.isDriving = true
self.markStartOfTrip()
}
if !activity.automotive &&
(activity.confidence == .high) &&
self.isDriving {
self.isDriving = false
self.markEndOfTrip()
}
}
}