I'm following the suggestions given in this answer. Still my timer is sometimes not nil
func recreateTimer(){
let timerLog = OSLog(subsystem: "LocationLogger", category: "Timer")
guard shouldUseTimer == true else {
os_log("we are using CoreMotion, no need to set timer", log: timerLog, type: .error)
return
}
if dwellingTimer != nil{
dwellingTimer?.invalidate()
dwellingTimer = nil
}
lastDwelling = lastLocation
os_log("Moved more than 160 | invalidated previous timer began new timer", log: timerLog, type: .error)
DispatchQueue.main.async { [weak self] in
guard self?.dwellingTimer == nil else{
os_log("Timer was not niled!!!", log: timerLog, type: .error)
return
}
self?.dwellingTimer = Timer.scheduledTimer(withTimeInterval: 60, repeats: false, block: { timer in
os_log("we reached 1 minute of no movement",log: timerLog, type: .fault)
self?.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
self?.beginMotionTracking()
os_log("Timer is deallocated & invalidated | Accuracy: %{public}f | RemainingTime: %{public}f",log: timerLog, type: .fault,(self?.locationManager.desiredAccuracy)!, UIApplication.shared.remainingTime())
})
}
os_log("New Timer is 'likely' set now", log: timerLog, type: .error)
}
Sometimes I get hits on
os_log("we are using CoreMotion, no need to set timer", log: timerLog, type: .error).